-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorder_by_exercises.sql
89 lines (74 loc) · 2.33 KB
/
order_by_exercises.sql
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
USE employees;
# Modify your first query to order by first name.
# The first result should be Irena Flexer
# and the last result should be Vidya Awdeh.
SELECT *
FROM employees
WHERE first_name IN ('Irena', 'Vidya', 'Maya')
ORDER BY first_name ASC;
# Update the query to order by first name and then last name.
# The first result should now be
# Irena Acton and the last should be Vidya Zweizig.
SELECT *
FROM employees
WHERE first_name IN ('Irena', 'Vidya', 'Maya')
ORDER BY first_name, last_name;
# Change the ORDER BY clause so that you
# order by last name before first name.
# Your first result should still be
# Irena Acton but now the last result should be Maya Zyda.
SELECT *
FROM employees
WHERE first_name IN ('Irena', 'Vidya', 'Maya')
ORDER BY last_name, first_name;
# Update your queries for employees with 'e'
# in their last name to sort the results by
# their employee number. Make sure the employee
# numbers are in the correct order.
SELECT *
FROM employees
WHERE last_name LIKE '%e%'
ORDER BY emp_no;
# Now reverse the sort order for both queries and compare results.
SELECT *
FROM employees
WHERE last_name LIKE '%e%'
OR first_name IN ('Irena', 'Vidya', 'Maya')
ORDER BY emp_no DESC, last_name DESC, first_name DESC;
SELECT last_name
FROM employees
WHERE last_name LIKE 'E%';
# Find all employees with a 'q' in their last name — 1,873 rows.
SELECT last_name
FROM employees
WHERE last_name LIKE '%q%';
# Update your query for 'Irena', 'Vidya',
# or 'Maya' to use OR instead of IN — 709 rows.
SELECT *
FROM employees
WHERE first_name LIKE 'Irena'
OR first_name LIKE 'Vidya'
OR first_name LIKE 'Maya';
# Add a condition to the previous query to
# find everybody with those names who is also male — 441 rows.
SELECT first_name
FROM employees
WHERE gender = 'M'
AND first_name IN ('Irena', 'Vidya', 'Maya');
# Find all employees whose last name starts or ends with 'E' — 30,723 rows.
SELECT *
FROM employees
WHERE last_name like 'E%'
OR last_name like '%E';
# Duplicate the previous query and update it
# to find all employees whose last name
# starts and ends with 'E' — 899 rows.
SELECT *
FROM employees
WHERE last_name like 'E%'
AND last_name like '%E';
# Find all employees with a 'q' in their last name but not 'qu' — 547 rows.
SELECT *
FROM employees
WHERE last_name LIKE '%q%'
AND last_name NOT LIKE '%qu%';