-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-SQL_Commands_Explained.sql
268 lines (226 loc) · 7.67 KB
/
1-SQL_Commands_Explained.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
/*
-- SQL Command Groups:
-----------------------------------
-- 1. Data Definition Language (DDL):
-- These commands define or modify the structure of a database or its objects.
-- Examples:
CREATE: Used to create a new database or table.
CREATE TABLE Employees (ID INT, Name VARCHAR(50), Age INT);
ALTER: Used to modify an existing table structure.
ALTER TABLE Employees ADD Salary DECIMAL(10, 2);
DROP: Used to delete a database, table, or other objects.
DROP TABLE Employees;
TRUNCATE: Removes all rows from a table but keeps the structure.
TRUNCATE TABLE Employees;
-----------------------------------
-- 2. Data Manipulation Language (DML):
-- These commands manipulate the data inside tables.
-- Examples:
SELECT: Retrieves data from one or more tables.
SELECT * FROM Employees;
INSERT: Adds new records to a table.
INSERT INTO Employees (ID, Name, Age) VALUES (1, 'John', 30);
UPDATE: Modifies existing records in a table.
UPDATE Employees SET Age = 35 WHERE ID = 1;
DELETE: Removes specific records from a table.
DELETE FROM Employees WHERE Age > 30;
-----------------------------------
-- 3. Transaction Control Language (TCL):
-- These commands manage transactions in a database.
-- Examples:
COMMIT: Saves changes made by a transaction permanently.
COMMIT;
ROLLBACK: Undoes changes made by a transaction.
ROLLBACK;
SAVEPOINT: Sets a point to which a transaction can be rolled back.
SAVEPOINT SP1;
-----------------------------------
-- 4. Data Control Language (DCL):
-- These commands control access to data in the database.
-- Examples:
GRANT: Grants privileges to a user.
GRANT SELECT ON Employees TO User1;
REVOKE: Removes privileges from a user.
REVOKE SELECT ON Employees FROM User1;
-----------------------------------
-- 5. Data Query Language (DQL):
-- This group technically includes only the SELECT command.
-- Example:
SELECT * FROM Employees;
*/
----------------------------------------------------------------------------------------------------------------------------------
-- Create a new database
-- CREATE DATABASE is used to create a new database
CREATE DATABASE Revision;
-- Use the newly created database
-- USE specifies which database to operate on
USE Revision;
-- Modify the database name
-- ALTER DATABASE with MODIFY NAME changes the database name
ALTER DATABASE Revision
MODIFY Name = Company
-- Delete the database permanently
-- DROP DATABASE removes the entire database
DROP DATABASE Company;
-- Create a new table
-- CREATE TABLE defines a new table with specified columns and data types
/*
CREATE TABLE table_name (
column1 datatype constraints,
column2 datatype constraints,
column3 datatype constraints,
...
);
constraints: (ex: PRIMARY KEY, NOT NULL, UNIQUE, etc).
*/
CREATE TABLE Customers(
First_Name nchar(100),
Second_Name char(100),
Age int
);
-- Insert data into the table
-- INSERT INTO adds a new row to the specified table
INSERT INTO Customers (First_Name, Second_Name, Age)
VALUES ('Menna', 'Muhammad', 23);
-- Add a new column to the table
-- ALTER TABLE with ADD adds a new column to an existing table
ALTER TABLE Customers
ADD Email char;
-- To change the data type of a column
-- ALTER TABLE with ALTER COLUMN modifies the column's data type
ALTER TABLE Customers
ALTER COLUMN Age VARCHAR(10);
-- Delete the entire table
-- DROP TABLE removes the specified table and its data
DROP TABLE Customers;
-- Create a new table with additional attributes
-- CREATE TABLE defines a table with multiple columns and data types
CREATE TABLE Employees (
First_Name NVARCHAR(50),
Second_Name NVARCHAR(50),
Age INT,
Salary DECIMAL(10, 2)
);
-- Insert multiple rows into the Employees' table
-- INSERT INTO adds multiple rows at once
INSERT INTO Employees (First_Name, Second_Name, Age, Salary)
VALUES
('Sameh', 'Jhon', 26, 19000),
('Alaa', 'Nabil', 25, 9000),
('Mahmoud', 'Essam', 26, 15000),
('Maha', 'Ahmed', 23, 11000),
('Osama', 'Mohamed', 57, 17000),
('Noha', 'Fathy', 48, 20000),
('Marawan', 'Sayed', 50, 28000),
('Nabil', 'Emad', 39, 21000),
('Nabil', 'Emad', 39, 21000),
('Nabil', 'Emad', 39, 21000);
-- Display all data from the table
-- SELECT * retrieves all rows and columns
SELECT *
FROM Employees;
-- Display specific columns only
-- SELECT retrieves specified columns
SELECT First_Name
FROM Employees;
-- Display multiple specific columns
SELECT First_Name,Age
FROM Employees;
-- Display rows matching a condition
-- WHERE filters rows based on a condition
SELECT First_Name,Second_Name
FROM Employees
WHERE First_Name ='Maha';
-- Rename the table
-- sp_rename is used to rename a table
-- Unlike renaming a database, where we can use the ALTER command, renaming a table cannot be done with ALTER.
-- Instead, we use the 'sp_rename' stored procedure to rename a table.
-- Example: sp_rename 'OldTableName', 'NewTableName';
sp_rename 'Employees', 'NewEmployees';
sp_rename 'NewEmployees', 'Employees';
/*
-- Comparison between DROP and DELETE:
-- DROP: Permanently removes an entire database, table, or other database objects.
-- It completely deletes the structure along with all data and cannot be rolled back.
-- DROP is part of Data Definition Language (DDL).
-- Example: DROP DATABASE + NAME OF THE DATABASE;
or
DROP TABLE + NAME OF THE TABLE;
--
-- DELETE: Removes specific rows from a table based on a condition using the WHERE clause.
-- It does not delete the table structure, and data removal can be rolled back if wrapped in a transaction.
-- DELETE is part of Data Manipulation Language (DML).
-- Example: DELETE FROM Employees WHERE Age > 30;
*/
-- Delete specific rows from the table
DELETE
FROM Employees
WHERE First_Name = 'Alaa';
-- Update specific values in the table
-- UPDATE modifies values based on a condition using where
-- SET specifies the new value
UPDATE Employees
SET Second_Name = 'essam'
WHERE Second_Name = 'Essam';
-- Use AND/OR for multiple conditions
-- AND requires all conditions to be true, OR requires at least one to be true
UPDATE Employees
SET Age = 23
WHERE First_Name = 'Sameh' And Second_Name = 'Jhon';
-- Display data without duplicates use distinct after select
-- DISTINCT eliminates duplicate rows
SELECT DISTINCT *
FROM Employees;
SELECT DISTINCT Second_Name, Age
FROM Employees;
-- Count the number of rows or values
-- COUNT calculates the number of rows or non-NULL values
SELECT COUNT(Age)
FROM Employees;
-- Count distinct values
SELECT COUNT( distinct Age)
FROM Employees;
-- Perform aggregate calculations
-- SUM / AVG / MIN / MAX calculate total, average, minimum, and maximum values respectively
SELECT SUM(Age)
FROM Employees;
-- Display rows with a specific value
SELECT *
FROM Employees
WHERE First_Name = 'Mahmoud';
-- Display rows with multiple specific values
-- IN checks for multiple possible values
SELECT *
FROM Employees
WHERE First_Name IN ('Osama', 'Maha', 'Mahmoud');
-- Search for patterns using wildcards
-- LIKE searches for patterns with % representing zero or more characters
SELECT *
FROM Employees
WHERE First_Name LIKE 'm%';
SELECT *
FROM Employees
WHERE First_Name LIKE '%a';
SELECT *
FROM Employees
WHERE First_Name LIKE '%a%';
-- Use comparison operators
-- > / < / <> / >= / <= compare values
SELECT *
FROM Employees
WHERE Age > 23;
-- Filter data within a range
-- BETWEEN specifies a range of values
SELECT *
FROM Employees
WHERE Age BETWEEN 23 and 29;
-- [ORDER BY] sorts rows in ascending (ASC) or descending (DESC) order
select *
from Employees
where Age between 23 and 29
ORDER BY Salary DESC;
-- Sort results by multiple columns
SELECT First_Name, Salary
FROM Employees
WHERE Age <> 23
ORDER BY Salary , First_Name;