SQL Notes - Part 1 and Part 2 Merged Notes.
SQL Notes - Part 1 and Part 2 Merged Notes.
Short Notes !
DATABASE
By @Curious_.programmer
r-e@curlous_.progrMHINII'
What is SQL?
• sql is stand for structured query language.
DML DCL
Doto Mompulotlon Doto Control TronsacUon Control
longuoge longuage Languoge
INSERT GRANT
ALTER REVOKE
UPDATE
SAVEPOINT
DROP DELETE
TRUNCATE
® @curlous_.pragnll!IW
DDL COMMANDS:
• DDL (Data Definition Language) used to change the structure of the
table Like creating the table, altering the table 8. Deleting the table.
• All the commands in the DDL are auto Committed that means it
permanently saves all the changes in the database.
1. CREATE~
this command is used to create a new database or table.
syntax:
CREATE TABLE table_name (
columnl datatype,
column2 datatype,
column3 datatype,
);
Example:
CREA TE TABLE Employee
(
EmployeelD int,
FirstName varchar(255),
LastName varchar(255),
Addressline varchar(255),
City varchar(255)
);
I @ 0curtous_.progranmer
2. Alter
The ALTER TABLE statement in Structured Query Language allows
you to add, modify, and delete columns of an existing table.
Syntax:
ALTER TABLE table name
ADD column _name datatype;
Example:
ALTER TABLE Employee
ADD Email varchar(255);
3. DrO(P
The DROP TABLE statement is used to drop an existing table in a
database.
this command deletes both the structure&. Records Stored in table.
syntax:
DROP TABLE table_name;
Example:
Drop TABLE Employee
lt l ,, ~ , t
4. TRUNCATE
A truncate SQL statement is used to remove all rows (complete data) from
a table. It is similar to the DELETE statement with no WHERE clause.
Syntax:
TRUNCATE TABLE table_name;
Example:
TRUNCATE TABLE Employee;
DML COMMANDS:
1.. INSERT
SQL INSERT statement is a SQL query. It is used to insert a single or a
multiple records in a table.
syntax:
INSERT INTO table name
VALUES (valuel, value2, value3 .... ) ;
Example:
INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)
VALUES (1, Yadnyesh , 19, PUNE) ;
lt l ,, ~ , t
2. UPDATE
The UPDATE statement is used to modify the existing records in a table.
Syntax:
UPDATE table name
SET columnl = valuel, column2 = value2, ...
WHERE condition;
Example:
UPDATE Customers
SET ContactName = 'Yadu', City= 'pune'
WHERE CustomerlD = 101;
3. DELETE
Syntax:
DELETE FROM table_name [WHERE condition] ;
Example:
DELETE FROM Customers WHERE CustomerName='Yadu";
I @ 0curtous_.prograllllll8F
DCL COMMANDS:
1. GRANT
It is used to give user access privileges to a database.
syntax:
GRANT SELECT, UPDATE ON MY_ TABLE TO SOME_ USER,
ANOTHER_USER ;
2. REVOKE
Syntax:
REVOKE SELECT, UPDATE ON MY_ TABLE FROM USERl, USER2;
® @curlous_.progrMNMr
TCL COMMANDS:
1. COMMIT
Commits a Transaction. The COMMIT command saves all the
transactions to the database since the last COMMIT or ROLLBACK
command.
Syntax:
COMMIT;
Example:
DELETE FROM Student WHERE AGE = 20;
COMMIT;
2. ROLLBACK
If any error occurs with any of the SQL grouped statements, all
changes need to be aborted. The process of reversing changes is
called rollback
Syntax:
ROLLBACK;
Example:
DELETE FROM Student WHERE AGE = 20;
ROLLBACK;
I @ @curlous_.prograllllMF
SQL
Short Notes !
Part 2
J'AB
By @Curious_.programmer
® @curlous_.prograllllller
SQL OPERATOR
DEMO TABLE
® @curlous_.progl'Mllller
LOGICAL OPERATOR
1. AND OPERATOR
QUERY>
SELECT *FROM Student WHERE NAME="Vadnyesh" AND
City="Mumbai";
Output>
-
Roll No Name Class DIVISION City
® @curlous_.pragnllllW
2. OR OPERATOR
QUARY>
SELECT *FROM Student WHERE DIVISION="C" OR City="Delhi";
Output>
-
Roll No Name Class DIVISION City
® @curlous_.prognlllllll8r
3. IBETWEIEN OPERATOR
The BETWEEN operator in SQL shows the record within
the range mentioned in the SQL query. This operator
operates on the numbers, characters, and date/time
operands.
QUERY>
SELECT * FROM Student WHERE Roll No BETWEEN 102
AND 104:
Output>
-
Roll No Name Class DIVISION City
Note: BETWEEN returns all the values from given start record to
end records
® @curlous_.prognlllllll8r
4. LIKE OPERATOR
It filters the records from the columns based on the
pattern specified in the SQL query. LIKE is used in the
WHERE clause with the following three statements:
l. SELECT Statement
2. UPDATE Statement
3. DELETE Statement
There are two wildcards often used in conjunction with
the LIKE operator:
• The percent sign (%) represents zero, one, or multiple
characters
• The underscore sign (_) represents one, single
character
QUERY:
SELECT* FROM Student WHERE NAME LIKE 'V%';
output:
-
Roll No Name Class DIVISION City
® @curlous_.prognlllllll8r
QUERY:
SELECT* FROM Student WHERE CITY LIKE '_u%';
output:
-
Roll No Name Class DIVISION City
® @curlous_.prograllllMr
5. NOT OPERATOR
NOT operator in SQL shows those records from the table
where the criteria is not met. NOT operator is used with
where clause in a SELECT query.
Query:
SELECT * FROM Students WHERE NOT City = "Mumbai";
Output:
-
Roll No Name Class DIVISION City
® @cllrlous_.prognlllllller
5. IN OPERATOR
When we want to check for one or more than one value in
a single SQL query, we use IN operator with the WHERE
clause in a SELECT query.
Query:
SELECT* FROM Students WHERE City IN("Delhi","Pune");
Output:
-
Roll No Name Class DIVISION City
® @curlous_.prognlllllll8r
PDF uploaded on
Telegram
(Link in it))
(Both Part 1 & 2 Uploaded)
(1J
-
Python Coder
I ® @curlous_.progralllllNII