Repository navigation
Expand file tree
/
Copy pathDQL.sql
More file actions
120 lines (83 loc) · 2.08 KB
/
Copy pathDQL.sql
File metadata and controls
120 lines (83 loc) · 2.08 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
USE ecom_db;
-- Print the whole table
SELECT * FROM dim_customer;
-- Print only specific columns from a table
SELECT
customer_id, phone
FROM
dim_customer;
-- LIMIT COMAND : Returns only the n number of rows (in this case n is only 5 rows)
SELECT *
FROM
dim_customer
LIMIT 5;
-- WHERE Clause : it is passed as a condition to queries to filter (prune) the data.
SELECT *
FROM
dim_customer
WHERE
gender = 'M';
-- Using Multiple condition under where clause
SELECT *
FROM
dim_customer
WHERE
((state = 'montana') AND (gender = 'M')) AND (join_date < '2023-03-26');
-- LIKE operator : Matches the column values and returns them if they follow the given pattern
-- QNS : Print the all entries where name starts from M
SELECT *
FROM
dim_customer
WHERE
first_name LIKE ('m%');
-- QNS : Print the all entries where name starts from M and its third character is u and last character is n
SELECT *
FROM
dim_customer
WHERE
first_name LIKE ('m_u%n');
-- Sort the entries in dim_product table , by default the ORDER BY clause sorts entries in ASCENDING(Increasing order -> lower to high)
SELECT *
FROM
dim_product
ORDER BY
unit_price
DESC;
-- QNS FIND TOP 3 Expensive Products in dim_product (Using LIMIT, WIll use Window Functions in FUTURE Learning)
SELECT *
FROM
dim_product
ORDER BY
unit_price
DESC
LIMIT 3;
-- GROUPING : calculate avg of unit_price on basis of per category in dim_product Table
SELECT
category,
AVG(unit_price) AS "average_unit_price",
SUM(unit_price) AS "total_price"
FROM
dim_product
GROUP BY
category
ORDER BY
total_price
ASC
-- AS : Alias for a given column
SELECT category AS "Categories", unit_price
FROM
dim_product
-- HAVING : Use having clause when the column is not present in the table, and the column is beaing created in runtime using AS keyword
SELECT
category,
AVG(unit_price) AS "average_unit_price",
SUM(unit_price) AS "total_price"
FROM
dim_product
GROUP BY
category
HAVING
total_price < '40373'
ORDER BY
total_price
ASC