Find out the popular domains from a MySQL table with domain records and search volume

MySQLMySQLi Database

For this, you can use GROUP BY along with the ORDER BY clause. Let us first create a table &mins;

mysql> create table DemoTable
   -> (
   -> URL varchar(40),
   -> DomainName varchar(20),
   -> SearchTimes int
   -> );
Query OK, 0 rows affected (0.62 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('www.gmail.com','gmail.com',4);
Query OK, 1 row affected (0.26 sec)
mysql> insert into DemoTable values('www.google.com','google.com',3);
Query OK, 1 row affected (0.25 sec)
mysql> insert into DemoTable values('www.gmail.com','gmail.com',9);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('www.facebook.com','facebook.com',8);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('www.facebook.com','facebook.com',2);
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+------------------+--------------+-------------+
| URL              | DomainName   | SearchTimes |
+------------------+--------------+-------------+
| www.gmail.com    | gmail.com    |           4 |
| www.google.com   | google.com   |           3 |
| www.gmail.com    | gmail.com    |           9 |
| www.facebook.com | facebook.com |           8 |
| www.facebook.com | facebook.com |           2 |
+------------------+--------------+-------------+
5 rows in set (0.00 sec)

Here is the query to find out the popular domains by displaying only the domains with the highest search volume. The search time is calculated for every domain and the one with the highest search volumes are displayed −

mysql> select DomainName,sum(SearchTimes) as TotalSearch from DemoTable
   -> group by DomainName
   -> order by TotalSearch desc;

This will produce the following output −

+--------------+-------------+
| DomainName   | TotalSearch |
+--------------+-------------+
| gmail.com    |          13 |
| facebook.com |          10 |
| google.com   |           3 |
+--------------+-------------+
3 rows in set (0.00 sec)
raja
Published on 17-Dec-2019 07:34:46
Advertisements