-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_basic_queries.pgsql
73 lines (66 loc) · 1 KB
/
1_basic_queries.pgsql
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
-- SELECT and FROM commands
SELECT
*
FROM
sales.customers;
-- WHERE, IN
SELECT
*
FROM
sales.customers
WHERE
customer_id IN (10, 20, 30);
-- BETWEEN, AND
SELECT
*
FROM
sales.customers
WHERE
customer_id BETWEEN 10 AND 30;
-- AND, LIKE
SELECT
*
FROM
sales.customers
WHERE
customer_id BETWEEN 10 AND 30
AND first_name LIKE 'A%';
-- ORDER BY to sort the result set
SELECT
*
FROM
sales.customers
WHERE
customer_id BETWEEN 10 AND 30
AND first_name LIKE 'A%'
ORDER BY
first_name DESC,
last_name ASC;
-- LIMIT to limit the number of rows returned
SELECT
*
FROM
sales.customers
LIMIT 10;
-- GROUP BY to group the result set
SELECT
customer_id,
SUM(total_amount) AS total
FROM
sales.orders
GROUP BY
customer_id
ORDER BY
total DESC;
-- HAVING to filter the result set
SELECT
customer_id,
SUM(total_amount) AS total
FROM
sales.orders
GROUP BY
customer_id
HAVING
SUM(total_amount) > 5000
ORDER BY
total DESC;