FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sql-ultimate-course/scripts/01_Query_Data_SELECT.sql at main · sindoc/sql-ultimate-course · GitHub
sindoc
/
sql-ultimate-course
Public
forked from
DataWithBaraa/sql-ultimate-course
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
sql-ultimate-course
/
scripts
/
01_Query_Data_SELECT.sql
Copy path
More file actions
More file actions
Latest commit
History
History
History
235 lines (195 loc) · 6.42 KB
Breadcrumbs
sql-ultimate-course
/
scripts
/
01_Query_Data_SELECT.sql
Copy path
File metadata and controls
235 lines (195 loc) · 6.42 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
==============================================================================
SQL SELECT Query
-------------------------------------------------------------------------------
This guide covers various SELECT query techniques used for retrieving,
filtering, sorting, and aggregating data efficiently.
Table of Contents:
1. SELECT ALL COLUMNS
2. SELECT SPECIFIC COLUMNS
3. WHERE CLAUSE
4. ORDER BY
5. GROUP BY
6. HAVING
7. DISTINCT
8. TOP
9. Combining Queries
10. COOL STUFF - Additional SQL Features
=================================================================================
*/
/*
==============================================================================
COMMENTS
===============================================================================
*/
--
This is a single-line comment.
/*
This
is
a multiple-line
comment
*/
/*
==============================================================================
SELECT ALL COLUMNS
===============================================================================
*/
--
Retrieve All Customer Data
SELECT
*
FROM
customers
--
Retrieve All Order Data
SELECT
*
FROM
orders
/*
==============================================================================
SELECT FEW COLUMNS
===============================================================================
*/
--
Retrieve each customer's name, country, and score.
SELECT
first_name,
country,
score
FROM
customers
/*
==============================================================================
WHERE
===============================================================================
*/
--
Retrieve customers with a score not equal to 0
SELECT
*
FROM
customers
WHERE
score
!=
0
--
Retrieve customers from Germany
SELECT
*
FROM
customers
WHERE
country
=
'
Germany
'
--
Retrieve the name and country of customers from Germany
SELECT
first_name,
country
FROM
customers
WHERE
country
=
'
Germany
'
/*
==============================================================================
ORDER BY
===============================================================================
*/
/*
Retrieve all customers and
sort the results by the highest score first.
*/
SELECT
*
FROM
customers
ORDER BY
score
DESC
/*
Retrieve all customers and
sort the results by the lowest score first.
*/
SELECT
*
FROM
customers
ORDER BY
score
ASC
/*
Retrieve all customers and
sort the results by the country.
*/
SELECT
*
FROM
customers
ORDER BY
country
ASC
/*
Retrieve all customers and
sort the results by the country and then by the highest score.
*/
SELECT
*
FROM
customers
ORDER BY
country
ASC
, score
DESC
/*
Retrieve the name, country, and score of customers
whose score is not equal to 0
and sort the results by the highest score first.
*/
SELECT
first_name,
country,
score
FROM
customers
WHERE
score
!=
0
ORDER BY
score
DESC
/*
==============================================================================
GROUP BY
===============================================================================
*/
--
Find the total score for each country
SELECT
country,
SUM
(score)
AS
total_score
FROM
customers
GROUP BY
country
/*
This will not work because 'first_name' is neither part of the GROUP BY
nor wrapped in an aggregate function. SQL doesn't know how to handle this column.
*/
SELECT
country,
first_name,
SUM
(score)
AS
total_score
FROM
customers
GROUP BY
country
--
Find the total score and total number of customers for each country
SELECT
country,
SUM
(score)
AS
total_score,
COUNT
(id)
AS
total_customers
FROM
customers
GROUP BY
country
/*
==============================================================================
HAVING
===============================================================================
*/
/*
Find the average score for each country
and return only those countries with an average score greater than 430
*/
SELECT
country,
AVG
(score)
AS
avg_score
FROM
customers
GROUP BY
country
HAVING
AVG
(score)
>
430
/*
Find the average score for each country
considering only customers with a score not equal to 0
and return only those countries with an average score greater than 430
*/
SELECT
country,
AVG
(score)
AS
avg_score
FROM
customers
WHERE
score
!=
0
GROUP BY
country
HAVING
AVG
(score)
>
430
/*
==============================================================================
DISTINCT
===============================================================================
*/
--
Return Unique list of all countries
SELECT DISTINCT
country
FROM
customers
/*
==============================================================================
TOP
===============================================================================
*/
--
Retrieve only 3 Customers
SELECT
TOP
3
*
FROM
customers
--
Retrieve the Top 3 Customers with the Highest Scores
SELECT
TOP
3
*
FROM
customers
ORDER BY
score
DESC
--
Retrieve the Lowest 2 Customers based on the score
SELECT
TOP
2
*
FROM
customers
ORDER BY
score
ASC
--
Get the Two Most Recent Orders
SELECT
TOP
2
*
FROM
orders
ORDER BY
order_date
DESC
/*
==============================================================================
All Together
===============================================================================
*/
/*
Calculate the average score for each country
considering only customers with a score not equal to 0
and return only those countries with an average score greater than 430
and sort the results by the highest average score first.
*/
SELECT
country,
AVG
(score)
AS
avg_score
FROM
customers
WHERE
score
!=
0
GROUP BY
country
HAVING
AVG
(score)
>
430
ORDER BY
AVG
(score)
DESC
/*
==============================================================================
COOL STUFF - Additional SQL Features
===============================================================================
*/
--
Execute multiple queries at once
SELECT
*
FROM
customers;
SELECT
*
FROM
orders;
/*
Selecting Static Data
*/
--
Select a static or constant value without accessing any table
SELECT
123
AS
static_number;
SELECT
'
Hello
'
AS
static_string;
--
Assign a constant value to a column in a query
SELECT
id,
first_name,
'
New Customer
'
AS
customer_type
FROM
customers;
Back
|
FazBrowse Home
|
New Git URL