-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimit_exercises.sql
More file actions
70 lines (61 loc) · 1.58 KB
/
limit_exercises.sql
File metadata and controls
70 lines (61 loc) · 1.58 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
-- Exercise 1
-- List the first 10 distinct last name sorted in descending order.
-- Zykh
-- Zyda
-- Zwicker
-- Zweizig
-- Zumaque
-- Zultner
-- Zucker
-- Zuberek
-- Zschoche
-- Zongker
SELECT DISTINCT last_name
FROM employees
ORDER BY last_name DESC
LIMIT 10;
-- Grouping by the same column you're selecting is like SELECT DISTINCT column then ordering by that column
SELECT last_name
FROM employees
GROUP BY last_name DESC
LIMIT 10;
-- Find your query for employees born on Christmas and hired in the 90s
-- from order_by_exercises.sql.
-- Update it to find just the first 5 employees.
SELECT *
FROM employees
WHERE hire_date like '199%'
AND birth_date like '%-12-25'
ORDER BY birth_date ASC, hire_date DESC
LIMIT 5;
-- SELECT *
-- FROM employees
-- WHERE hire_date BETWEEN '1990-01-01' AND '1999-12-31'
-- Think of your results as batches, sets, or pages.
-- The first five results are your first page.
-- The five after that would be your second page, etc.
-- Update the query to find the tenth page of results.
-- The employee names should be:
-- Piyawadee Bultermann
-- Heng Luft
-- Yuqun Kandlur
-- Basil Senzako
-- Mabo Zobel
SELECT *
FROM employees
WHERE hire_date like '199%'
AND birth_date like '%-12-25'
ORDER BY birth_date ASC, hire_date DESC
LIMIT 5 OFFSET (10 - 1)*5;
-- page | limit | offset
-- 1 5 0
-- 2 5 5
-- 3 5 10
-- 4 5 15
-- 5 5 20
-- 6 5 25
-- 7 5 30
-- 8 5 35
-- 9 5 40
-- 10 5 45
-- offset = (page - 1) * limit