-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask4.sql
More file actions
98 lines (68 loc) · 1.73 KB
/
Copy pathTask4.sql
File metadata and controls
98 lines (68 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
CREATE DATABASE task4;
USE task4;
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
age INT,
email VARCHAR(100),
department VARCHAR(50)
);
INSERT INTO students (name, age, email, department) VALUES
('Vijay', 20, 'vijay@gmail.com', 'CSE'),
('Arun', 22, 'arun@yahoo.com', 'ECE'),
('Priya', 21, 'priya@gmail.com', 'CSE'),
('Karthik', 23, NULL, 'MECH'),
('Anu', 19, 'anu@gmail.com', 'IT');
1️--Sort data using ORDER BY (ASC & DESC)
--Ascending (default)
SELECT * FROM students
ORDER BY age ASC;
--Descending
SELECT * FROM students
ORDER BY age DESC;
2️--Apply sorting on multiple columns
--First sorts by department, then by age
SELECT * FROM students
ORDER BY department ASC, age DESC;
3️--Limit results using LIMIT
--Show only top 3 rows
SELECT * FROM students
ORDER BY age DESC
LIMIT 3;
4️--Combine ORDER BY with WHERE
--Students from CSE sorted by age
SELECT * FROM students
WHERE department = 'CSE'
ORDER BY age ASC;
5️--Use OFFSET for pagination
--Page size = 2
-- Page 1
SELECT * FROM students
ORDER BY id
LIMIT 2 OFFSET 0;
-- Page 2
SELECT * FROM students
ORDER BY id
LIMIT 2 OFFSET 2;
6️--Understand performance impact (Interview point)
Sorting on indexed columns is faster(right)
Sorting on large tables without index is slow(wrong)
Example (optional):
CREATE INDEX idx_age ON students(age);
7️--Build a leaderboard-style query
--Oldest students first (like rank list)
SELECT name, age, department
FROM students
ORDER BY age DESC;
--Top 3 leaderboard
SELECT name, age
FROM students
ORDER BY age DESC
LIMIT 3;
8️--Test edge cases
--NULL values last
SELECT * FROM students
ORDER BY email IS NULL, email;
--Same age handling
SELECT * FROM students
ORDER BY age DESC, name ASC;