Skip to content

Commit be9b26e

Browse files
committed
1 parent 8846c3a commit be9b26e

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
SELECT student_name
2+
,SUM(CASE WHEN exam_id=1 THEN score END) AS exam_1
3+
,SUM(CASE WHEN exam_id=2 THEN score END) AS exam_2
4+
,SUM(CASE WHEN exam_id=3 THEN score END) AS exam_3
5+
,SUM(CASE WHEN exam_id=4 THEN score END) AS exam_4
6+
FROM exam_scores
7+
GROUP BY 1
8+
9+
10+
-- NOTE: solved in first attempt itself.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-- Solution 1: using HAVING clause
2+
3+
SELECT id, name, created_at
4+
FROM users
5+
GROUP BY 1, 2, 3
6+
HAVING COUNT(*) > 1
7+
8+
9+
-- Solution 2: using ROW_NUMBER() function
10+
11+
WITH CTE as (
12+
SELECT *, ROW_NUMBER() OVER(PARTITION BY id ORDER BY created_at) as rn
13+
FROM users
14+
)
15+
SELECT id, name, created_at
16+
FROM CTE
17+
WHERE rn > 1

0 commit comments

Comments
 (0)