Pacific-Design.com

    
Home Index

1. SQL Language

2. 02 GROUP BY

SQL Language / 02 GROUP BY /

SQL - GROUP BY

SELECT date, SUM(price) AS sum_price
FROM sales
GROUP BY date
ORDER BY date;

-- +------------+-----------+
-- | date       | sum_price |
-- +------------+-----------+
-- | 2017-01-03 |    300.00 |
-- | 2017-01-06 |     50.00 |
-- | 2017-01-08 |    180.00 |
-- | 2017-01-10 |      5.00 |
-- +------------+-----------+


SQL - Find Duplicate Emails

SELECT email, COUNT(email) AS total
FROM student
GROUP BY email
HAVING total > 1;


Top IP Addresses

SELECT ip, COUNT(ip) total
FROM keywords
WHERE ip LIKE "192%"
GROUP BY ip
HAVING total > 100
ORDER BY total DESC

-- +-------------+-------+
-- | ip          | total |
-- +-------------+-------+
-- | 180.179.201 | 82    |
-- | 174.142.104 | 22    |
-- | 148.251.41  | 16    |
-- | 192.99.19   | 13    |
-- +-------------+-------+


Reference: