-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimit_and_aliasing.sql
More file actions
204 lines (155 loc) · 4.67 KB
/
Copy pathlimit_and_aliasing.sql
File metadata and controls
204 lines (155 loc) · 4.67 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
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
-- =========================================================
-- FILE: limit_and_aliasing.sql
-- AUTHOR: Benard Onyango Omoga
-- DATABASE: MySQL
-- PROJECT: TechSphere Solutions Database System
-- DESCRIPTION:
-- Demonstrates LIMIT clause and column aliasing
-- using TechSphere database tables.
-- =========================================================
-- =========================================================
-- USE DATABASE
-- =========================================================
USE techsphere_solutions;
-- =========================================================
-- SECTION 1: LIMIT CLAUSE
-- PURPOSE:
-- LIMIT restricts the number of rows returned
-- in a query result.
-- =========================================================
-- Retrieve only the first 3 employees
SELECT *
FROM employees
LIMIT 3;
-- =========================================================
-- LIMIT WITH ORDER BY
-- =========================================================
-- Retrieve first 3 employees sorted alphabetically
SELECT *
FROM employees
ORDER BY first_name
LIMIT 3;
-- WHAT IT DOES:
-- 1. Sorts employees by first name (A-Z)
-- 2. Returns only the first 3 rows
-- =========================================================
-- LIMIT WITH OFFSET
-- =========================================================
-- Display all employees ordered by first name
SELECT *
FROM employees
ORDER BY first_name;
-- Retrieve 2 employees starting from position 3
SELECT *
FROM employees
ORDER BY first_name
LIMIT 3, 2;
-- WHAT IT DOES:
-- LIMIT 3,2 means:
-- Start from row position 3
-- Then return the next 2 rows
-- =========================================================
-- PRACTICAL ANALYTICS EXAMPLE
-- =========================================================
-- View employees ordered by hire date (oldest employees first)
SELECT *
FROM employees
ORDER BY hire_date;
-- Retrieve the third earliest hired employee
SELECT *
FROM employees
ORDER BY hire_date
LIMIT 2,1;
-- WHAT IT DOES:
-- 1. Sorts employees by hire date
-- 2. Skips first 2 rows
-- 3. Returns the next row only
-- =========================================================
-- SECTION 2: ALIASING
-- PURPOSE:
-- Aliasing temporarily renames columns or tables
-- for readability and cleaner output.
-- =========================================================
-- Average salary by gender
SELECT
gender,
AVG(base_salary)
FROM employees e
JOIN employee_positions ep
ON e.employee_id = ep.employee_id
JOIN job_roles jr
ON ep.role_id = jr.role_id
GROUP BY gender;
-- =========================================================
-- USING ALIAS WITH AS KEYWORD
-- =========================================================
SELECT
gender,
AVG(base_salary) AS average_salary
FROM employees e
JOIN employee_positions ep
ON e.employee_id = ep.employee_id
JOIN job_roles jr
ON ep.role_id = jr.role_id
GROUP BY gender;
-- WHAT IT DOES:
-- Renames AVG(base_salary) column to "average_salary"
-- =========================================================
-- ALIAS WITHOUT AS KEYWORD
-- =========================================================
SELECT
gender,
AVG(base_salary) average_salary
FROM employees e
JOIN employee_positions ep
ON e.employee_id = ep.employee_id
JOIN job_roles jr
ON ep.role_id = jr.role_id
GROUP BY gender;
-- WHAT IT DOES:
-- Same result as above
-- "AS" keyword is optional in MySQL
-- =========================================================
-- TABLE ALIASING
-- =========================================================
-- Using shorter table aliases for readability
SELECT
e.first_name,
e.last_name,
d.department_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id;
-- WHAT IT DOES:
-- e = employees
-- d = departments
-- Makes JOIN queries shorter and easier to read
-- =========================================================
-- SECTION 3: LIMIT + ORDER BY + ALIASING
-- =========================================================
-- Top 3 highest paying job roles
SELECT
role_title AS job_role,
base_salary AS salary
FROM job_roles
ORDER BY salary DESC
LIMIT 3;
-- WHAT IT DOES:
-- 1. Renames output columns
-- 2. Sorts salaries highest to lowest
-- 3. Returns top 3 highest-paying roles
-- =========================================================
-- KEY CONCEPT SUMMARY
-- =========================================================
-- LIMIT:
-- Restricts number of rows returned
-- OFFSET:
-- Specifies starting position in LIMIT
-- ALIAS:
-- Temporarily renames columns or tables
-- ORDER BY:
-- Sorts query results
-- =========================================================
-- END OF FILE
-- =========================================================
```