-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_DML.sql
More file actions
152 lines (117 loc) · 3.52 KB
/
2_DML.sql
File metadata and controls
152 lines (117 loc) · 3.52 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
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(100),
ContactName VARCHAR(100),
Address VARCHAR(100),
City VARCHAR(50),
PostalCode VARCHAR(20),
Country VARCHAR(50)
);
INSERT INTO Customers (CustomerID, CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES
(1, 'Alfreds Futterkiste', 'Maria Anders', 'Obere Str. 57', 'Berlin', '12209', 'Germany'),
(2, 'Ana Trujillo Emparedados y helados', 'Ana Trujillo', 'Avda. de la Constitución 2222', 'México D.F.', '05021', 'Mexico'),
(3, 'Antonio Moreno Taquería', 'Antonio Moreno', 'Mataderos 2312', 'México D.F.', '05023', 'Mexico'),
(4, 'Around the Horn', 'Thomas Hardy', '120 Hanover Sq.', 'London', 'WA1 1DP', 'UK'),
(5, 'Berglunds snabbköp', 'Christina Berglund', 'Berguvsvägen 8', 'Luleå', 'S-958 22', 'Sweden'),
(6, 'Supan Roy', 'Jonathan Byers', '223 University Ave', 'Dhaka', '1219', 'Bangladesh');
-- SELECT Columns
SELECT CustomerName, City, Country FROM Customers;
-- SELECT DISTINCT Values
SELECT DISTINCT Country, City FROM Customers;
-- Count Distinct Values
SELECT COUNT(DISTINCT Country) FROM Customers;
--WHERE Clause
SELECT * FROM Customers
WHERE Country = 'Mexico';
SELECT CustomerName FROM Customers
WHERE CustomerID = 1;
-- AND, OR and NOT Operators
SELECT * FROM Customers
WHERE Country = 'Germany' AND City = 'Berlin';
SELECT * FROM Customers
WHERE Country = 'Germany' OR Country = 'UK';
SELECT * FROM Customers
WHERE NOT Country = 'UK';
SELECT * FROM Customers
WHERE Country = 'Germany' AND (City = 'Berlin' OR City = 'Stuttgart');
-- Order By Descending
SELECT * FROM Customers
ORDER BY Country DESC;
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;
-- NULL/NOT NULL Values
SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NOT NULL;
-- Update Table
UPDATE Customers
SET ContactName = 'Alfred Schmidt'
WHERE CustomerID = 1;
-- UPDATE Multiple Records
UPDATE Customers
SET PostalCode = 52511
WHERE Country = 'Mexico';
-- DELETE Statement
DELETE FROM Customers WHERE CustomerID = 6;
-- LIMIT Clause
SELECT * FROM Customers
ORDER BY Country
LIMIT 3;
-- MIN() and MAX() Functions
SELECT MAX(CustomerID) AS LongestValue
FROM Customers;
-- COUNT(), AVG() and SUM() Functions
SELECT COUNT(CustomerID)
FROM Customers;
SELECT AVG(CustomerID)
FROM Customers;
SELECT SUM(CustomerID)
FROM Customers;
-- LIKE Operator
SELECT * FROM Customers
WHERE CustomerName LIKE 'A%';
SELECT * FROM Customers
WHERE CustomerName LIKE '_n%';
-- IN Operator
SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');
-- Between
SELECT * FROM Customers
WHERE CustomerID BETWEEN 2 AND 4;
-- Aliases
SELECT CustomerID AS Cid, CustomerName AS Cname
FROM Customer;
-- GROUP BY
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;
-- HAVING
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 1;
-- EXISTS Syntax demo
SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID = Suppliers.supplierID AND Price < 20);
-- ANY/ ALL demo
SELECT ProductName
FROM Products
WHERE ProductID = ANY
(SELECT ProductID
FROM OrderDetails
WHERE Quantity = 10);
SELECT ProductName
FROM Products
WHERE ProductID = ALL
(SELECT ProductID
FROM OrderDetails
WHERE Quantity = 10);
-- INSERT INTO SELECT Syntax
INSERT INTO Customers (CustomerName, City, Country)
SELECT SupplierName, City, Country FROM Suppliers;