forked from Rebecca-S-Liu/Feb2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLQuery_1.sql
277 lines (216 loc) · 8.58 KB
/
SQLQuery_1.sql
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
--mssqlsystemresource
USE Northwind
GO
--SELECT statement: identify which columns we want to retrieve
--1. SELECT all columns and rows
SELECT *
FROM Employees
--2. SELECT a list of columns
SELECT EmployeeID, FirstName, LastName, Title, ReportsTo
FROM Employees
SELECT e.EmployeeID, e.FirstName, e.LastName, e.Title, e.ReportsTo
FROM Employees AS e
--avoid using SELECT *
--1) unnecessary data
--2) name conflicts
SELECT *
FROM Employees
SELECT *
FROM Customers
SELECT *
FROM Employees e JOIN Orders o ON e.EmployeeID = o.EmployeeID JOIN Customers c ON o.CustomerID = c.CustomerID
--3. SELECT DISTINCT Value:
--list all the cities that employees located at
SELECT City
FROM Employees
SELECT DISTINCT City
FROM Employees
--4. SELECT combined with plain text: retrieve the full name of employees
SELECT FirstName + ' ' + LastName AS FullName
FROM Employees
--identifiers: names we give to db, tables, columns, sp.
--1) regular identifier: comply with the rules for the format of identifiers
--1) first character: a-z, A-Z, @, #
--@: declare a variable
DECLARE @today DATETIME
SELECT @today = GETDATE()
PRINT @today
--#: temp tables
--#: local temp table
--##: global temp table
--2) subsequent characters: a-z, A-Z, 0-9, @, $, #, _
--3) identifier must not be a sql reserved work, both uppercase or lower case
--SELECT MAX, AVG
--FROM TABLE
--4) embedded space are not allowed
--2) delimited identifier: [] ""
SELECT FirstName + ' ' + LastName AS "Full Name"
FROM Employees
SELECT *
FROM [Order Details]
--WHERE statement: filter records
--1. equal =
--Customers who are from Germany
SELECT ContactName, Country
FROM Customers
WHERE Country = 'Germany'
--Product which price is $18
SELECT ProductName, UnitPrice
FROM Products
WHERE UnitPrice = 18
--2. Customers who are not from UK
SELECT ContactName, Country
FROM Customers
WHERE Country != 'UK'
SELECT ContactName, Country
FROM Customers
WHERE Country <> 'UK'
--IN Operator: retrieve among a list of values
--E.g: Orders that ship to USA AND Canada
SELECT OrderID, CustomerID, ShipCountry
FROM Orders
WHERE ShipCountry = 'USA' OR ShipCountry = 'Canada'
SELECT OrderID, CustomerID, ShipCountry
FROM Orders
WHERE ShipCountry IN ('USA', 'Canada')
--BETWEEN Operator: retreive in a consecutive range, inclusive
--1. retreive products whose price is between 20 and 30.
SELECT ProductName, UnitPrice
FROM Products
WHERE UnitPrice >= 20 AND UnitPrice <= 30
SELECT ProductName, UnitPrice
FROM Products
WHERE UnitPrice BETWEEN 20 AND 30
--NOT Operator: display a record if the condition is NOT TRUE
-- list orders that does not ship to USA or Canada
SELECT OrderID, CustomerID, ShipCountry
FROM Orders
WHERE ShipCountry NOT IN ('USA', 'Canada')
SELECT OrderID, CustomerID, ShipCountry
FROM Orders
WHERE NOT ShipCountry IN ('USA', 'Canada')
SELECT ProductName, UnitPrice
FROM Products
WHERE UnitPrice NOT BETWEEN 20 AND 30
SELECT ProductName, UnitPrice
FROM Products
WHERE NOT UnitPrice BETWEEN 20 AND 30
--NULL Value: a field with no value
--check which employees' region information is empty
SELECT EmployeeID, FirstName, LastName, Region
FROM Employees
WHERE Region is null
--exclude the employees whose region is null
SELECT EmployeeID, FirstName, LastName, Region
FROM Employees
WHERE Region is not null
--Null in numerical operation
CREATE TABLE TestSalary(EId int primary key identity(1,1), Salary money, Comm money)
INSERT INTO TestSalary VALUES(2000, 500), (2000, NULL),(1500, 500),(2000, 0),(NULL, 500),(NULL,NULL)
SELECT EId, Salary, Comm, IsNull(Salary, 0) + IsNull(Comm, 0) AS TotalCompensation
FROM TestSalary
--LIKE Operator: create a serach expression
--1. Work with % wildcard character: % is used to substitute to 0 or more characters
--retrieve all the employees whose last name starts with D
SELECT FirstName, LastName
FROM Employees
WHERE LastName LIKE 'D%'
--2. Work with [] and % to search in ranges: find customers whose postal code starts with number between 0 and 3
SELECT ContactName, PostalCode
FROM Customers
WHERE PostalCode LIKE '[0-3]%'
--3. Work with NOT:
SELECT ContactName, PostalCode
FROM Customers
WHERE PostalCode NOT LIKE '[0-3]%'
--4. Work with ^: any characters not in the brackets
SELECT ContactName, PostalCode
FROM Customers
WHERE PostalCode LIKE '[^0-3]%'
--Custermer name starting from letter A but not followed by l-n
SELECT ContactName, PostalCode
FROM Customers
WHERE ContactName LIKE 'A[^l-n][a-l]%'
--ORDER BY statement: sort the result set in ascending or descending order
--1. retrieve all customers except those in Boston and sort by Name
SELECT ContactName, City
FROM Customers
WHERE City != 'Boston'
ORDER BY ContactName DESC
--2. retrieve product name and unit price, and sort by unit price in descending order
SELECT ProductName, UnitPrice
FROM Products
ORDER BY UnitPrice DESC
--3. Order by multiple columns
SELECT ProductName, UnitPrice
FROM Products
ORDER BY UnitPrice DESC, ProductName DESC
SELECT ProductName, UnitPrice
FROM Products
ORDER BY 2 DESC, 1 DESC
--JOIN: combine rows from tow or more tables, based on a related column between them
--1. INNER JOIN: will return the records that have matching values in both tables
--find employees who have deal with any orders
SELECT e.EmployeeID, e.FirstName + ' ' + e.LastName AS FullName, o.OrderDate
FROM Employees AS e INNER JOIN Orders AS o ON e.EmployeeID = o.EmployeeID
SELECT e.EmployeeID, e.FirstName + ' ' + e.LastName AS FullName, o.OrderDate
FROM Employees AS e JOIN Orders AS o ON e.EmployeeID = o.EmployeeID
SELECT e.EmployeeID, e.FirstName + ' ' + e.LastName AS FullName, o.OrderDate
FROM Employees e, Orders o
WHERE e.EmployeeID = o.EmployeeID
--get cusotmers information and corresponding order date
SELECT c.ContactName, c.City, c.Country, o.OrderDate
FROM Customers c INNER JOIN Orders o ON c.CustomerID = o.CustomerID
--join multiple tables:
--get customer name, the corresponding employee who is responsible for this order, and the order date
SELECT c.ContactName AS CustomerName, e.FirstName + ' ' + e.LastName AS EmployeeName, o.OrderDate
FROM Customers c INNER JOIN Orders o ON c.CustomerID = o.CustomerID INNER JOIN Employees e ON o.EmployeeID = e.EmployeeID
--add detailed information about quantity and price, join Order details
SELECT c.ContactName AS CustomerName, e.FirstName + ' ' + e.LastName AS EmployeeName, o.OrderDate, od.Quantity, od.UnitPrice
FROM Customers c INNER JOIN Orders o ON c.CustomerID = o.CustomerID INNER JOIN Employees e ON o.EmployeeID = e.EmployeeID INNER JOIN [Order Details] od ON od.OrderID = o.OrderID
--2. OUTER JOIN
--1) LEFT OUTER JOIN: return all records from the left table, and matching records from the right table, if no matching records, return null
--list all customers whether they have made any purchase or not
SELECT c.ContactName, o.OrderID
FROM Customers c LEFT JOIN Orders o ON c.CustomerID = o.CustomerID
ORDER BY o.OrderID DESC
--JOIN with WHERE: find out customers who have never placed any order
SELECT c.ContactName, o.OrderID
FROM Customers c LEFT JOIN Orders o ON c.CustomerID = o.CustomerID
WHERE o.OrderID is null
--2) RIGHT OUTER JOIN: return all records from the right table, and matching records from the left table, if no matching records, return null
SELECT c.ContactName, o.OrderID
FROM Orders o RIGHT JOIN Customers c ON c.CustomerID = o.CustomerID
ORDER BY o.OrderID
--3) FULL OUTER JOIN: return all rows from both left and right table with null values, if we cannot find mathcing records
--Match all customers and suppliers by country.
SELECT c.ContactName AS Customer, c.Country as CustomerCountry, s.Country as SupplierCountry, s.ContactName AS Supplier
FROM Customers c FULL JOIN Suppliers s ON c.Country = s.Country
ORDER BY CustomerCountry, SupplierCountry
--3. CROSS JOIN: create the cartesian product of two tables
--table1: 10 rows; table2: 20 rows -> cross join -> 200 rows
SELECT *
FROM Customers
SELECT *
FROM Orders
SELECT *
FROM Customers CROSS JOIN Orders
--* SELF JOIN:join a table with itself
SELECT EmployeeID, FirstName, LastName, ReportsTo
FROM Employees
--CEO: Andrew
--Manager: Nancy, Janet, Margaret, Steven, Laura
--Employee: Michael, Robert, Anne
--find emloyees with the their manager name
SELECT e.FirstName + ' ' + e.LastName AS Employee, m.FirstName + ' ' + m.LastName AS Manager
FROM Employees e INNER JOIN Employees m ON e.ReportsTo = m.EmployeeID
SELECT e.FirstName + ' ' + e.LastName AS Employee, m.FirstName + ' ' + m.LastName AS Manager
FROM Employees e LEFT JOIN Employees m ON e.ReportsTo = m.EmployeeID
--Batch Directives
CREATE DATABASE FebBatch
GO
USE FebBatch
GO
CREATE TABLE Employee(Id int, EName varchar(20), Salary money)
SELECT *
FROM Employee