Table of Contents
Last active
August 20, 2024 19:44
-
-
Save AndrewSavetchuk/41e5402459669d3a926905c9e93cf795 to your computer and use it in GitHub Desktop.
SELECT column_name from table_name GROUP BY column_name HAVING count(column_name) > 1;
Input:
Users table:
+----+---------+
| id | email |
+----+---------+
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
+----+---------+
Output:
+---------+
| email |
+---------+
| [email protected] |
+---------+
Explanation: [email protected] is repeated two times.
SELECT email from users GROUP BY email HAVING count(email) > 1;
Select second max value from the table. If there is no second highest value, the query should report null
.
Input:
+----+--------+
| id | value |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
Output:
+---------------------+
| SecondHighestValue |
+---------------------+
| 200 |
+---------------------+
Input:
+----+--------+
| id | value |
+----+--------+
| 1 | 100 |
+----+--------+
Output:
+---------------------+
| SecondHighestValue |
+---------------------+
| null |
+---------------------+
SELECT MAX(value) as SecondHighestValue FROM TableName WHERE value < (SELECT MAX(value) FROM TableName);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment