-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5-SQL-Keywords
More file actions
119 lines (67 loc) · 2.33 KB
/
5-SQL-Keywords
File metadata and controls
119 lines (67 loc) · 2.33 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
SQL Keywords & Functions
Introduction
These SQL keywords and functions help in modifying query results, filtering data, and working with date values.
-- 1️⃣ Aliasing (AS)
Aliasing means giving an alternate (temporary) name to a column or table in the result.
It improves readability of the output.
-- 2️⃣ DISTINCT
DISTINCT is used to return only unique values from a column.
It removes duplicate values.
-- 3️⃣ ORDER BY
ORDER BY is used to sort the result set based on one or more columns.
a) ASC (Ascending Order)
Default sorting order
Arranges data from A to Z
For numbers → smallest to largest
Sorts employee names from A to Z.
b) DESC (Descending Order)
Arranges data from Z to A
For numbers → largest to smallest
Sorts salary from highest to lowest.
-- 4️⃣ LIMIT
LIMIT is used to restrict the number of rows returned by a query.
Returns only first 5 rows.
-- 5️⃣ OFFSET
OFFSET is used to skip a specific number of rows before returning results.
-- 6️⃣ WHERE
WHERE is used to filter records based on specified conditions.
It is used to extract only those records that fulfill a specified condition.
-- 7️⃣ GROUP BY
GROUP BY is used to group rows that have the same values in specified columns into summary rows.
-- 8️⃣ HAVING
HAVING is used to filter groups based on a specified condition.
EXAMPLE:-
USE market_star_schema;
SELECT * FROM market_fact_full
ORDER BY sales;
SELECT * FROM cust_dimen
ORDER BY city ASC;
SELECT * FROM orders_dimen
ORDER BY order_date ASC;
SELECT * FROM market_fact_full
ORDER BY cust_id ASC, sales DESC;
-- find the highest sales amount order
SELECT * FROM market_fact_full
ORDER BY sales DESC
LIMIT 1;
-- find the order with second highest sales amount
SELECT * FROM market_fact_full
ORDER BY sales DESC
LIMIT 1 OFFSET 1;
-- find the five lowest amount sales orders
SELECT * FROM market_fact_full
ORDER BY sales
LIMIT 5;
-- find the fourth lowest profit value
SELECT * FROM market_fact_full
ORDER BY profit
LIMIT 1 OFFSET 3;
-- find the fourth highest sales value among the orders which has positive profit value
SELECT * FROM market_fact_full
WHERE profit>0
ORDER BY sales DESC
LIMIT 1 OFFSET 3;
-- how many distinct cities are present
SELECT COUNT(DISTINCT city) FROM cust_dimen;
-- find the distinct cities.
SELECT DISTINCT city FROM cust_dimen;