Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
140 views

SQL Notes - Part 1 and Part 2 Merged Notes.

The document provides an overview of SQL commands including DDL, DML, DCL commands and logical operators. It explains commands like CREATE, ALTER, DROP, TRUNCATE, INSERT, UPDATE, DELETE, GRANT, REVOKE along with logical operators like AND, OR, BETWEEN, LIKE, NOT and IN.

Uploaded by

Anish Dhamodaran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
140 views

SQL Notes - Part 1 and Part 2 Merged Notes.

The document provides an overview of SQL commands including DDL, DML, DCL commands and logical operators. It explains commands like CREATE, ALTER, DROP, TRUNCATE, INSERT, UPDATE, DELETE, GRANT, REVOKE along with logical operators like AND, OR, BETWEEN, LIKE, NOT and IN.

Uploaded by

Anish Dhamodaran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

SQL

Short Notes !

DATABASE

By @Curious_.programmer

r-e@curlous_.progrMHINII'
What is SQL?
• sql is stand for structured query language.

• This database language is mainly designed


for maintaining the data in relational
database management systems.

• sql is standard language for accessing and


manipulating database.

Types of SQL Conimands:


SQL
Commands

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

The DELETE statement is used to delete existing records in a table.

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

GRANT SELECT, UPDATE ON MY_ TABLE TO SOME_ USER,


ANOTHER_USER;

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

The SQL reserved words and characters are called


operators, which are used with a WHERE clause in a
SQL query.

DEMO TABLE

Roll No Name Class DIVISION City

101 Yadnyesh 10th A Pune


102 Om 10th C Mumbai
103 Sahil 10th D Pune
104 Rohan 10th 8 Pune
105 Sahil 10th A Delhi
106 Yadnyesh 10th C Mumbai

STUDENT INFORMATION TABLE

® @curlous_.progl'Mllller
LOGICAL OPERATOR
1. AND OPERATOR

The SQL AND operator is used with the where clause


in the SQL Query. AND operator in SQL returns only
those records which satisfy both the conditions in the
SQL query.

QUERY>
SELECT *FROM Student WHERE NAME="Vadnyesh" AND
City="Mumbai";

Output>

-
Roll No Name Class DIVISION City

106 Yadnyesh 10th C Mumbai

In above example the sql statement returns only one


value cause in Given table and their is only one student
who has name "yadnyesh" & he belongs to Mumbai.

Note: TRUE if all the conditions separated by AND is


TRUE

® @curlous_.pragnllllW
2. OR 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.
• If there is no value in the given range, then this operator
shows NULL value.

QUARY>
SELECT *FROM Student WHERE DIVISION="C" OR City="Delhi";
Output>

-
Roll No Name Class DIVISION City

102 Om 10th C Mumbai

105 Sahil 10th A Delhi

106 Yadnyesh 10th C Mumbai

In the above example the sql statement returns three


values. cause Given table have two Students who belongs
to C Division & one is from delhi.

Note: TRUE if any of the conditions separated by OR is TRUE

® @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

102 Om 10th C Mumbai

103 Sahil 10th D Pune

104 Rohan 10th B Pune

In the above example the sql statement returns three


values. cause in Roll_NO column there is only 3 Values
Lies From 102 to 104.

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

101 Yadnyesh 10th A Pune

101 Yadnyesh 10th A Pune

® @curlous_.prognlllllll8r
QUERY:
SELECT* FROM Student WHERE CITY LIKE '_u%';

output:

-
Roll No Name Class DIVISION City

101 Yadnyesh 10th A Pune

102 Om 10th C Mumbai

103 Sahil 10th D Pune

104 Rohan 10th B Pune

106 Yadnyesh 10th C Mumbai

In first Query returns all Name of students starting from


Y. In second Example returns all the records who have
second Latter is U then any combination of latter.

® @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

101 Yadnyesh 10th A Pune

103 Sahil 10th D Pune

104 Rohan 10th 8 Pune

105 Sahil 10th A Delhi

in the above example returns those student records


those who are not from mumbai. this query shows
records of students having City other than mumbai.

® @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

101 Yadnyesh 10th A Pune

103 Sahil 10th D Pune

104 Rohan 10th 8 Pune

105 Sahil 10th A Delhi

in the above example returns those student records


those who are from Delhi and Pune. other student will
not displayed.

® @curlous_.prognlllllll8r
PDF uploaded on
Telegram
(Link in it))
(Both Part 1 & 2 Uploaded)

(1J
-
Python Coder

Do io:n !or c0d1r:g resources Hand1,r1tten 1ot es & Ou1zzes 1 I

https ,,,t me/Cl,r1ous_Coder

Follow For Posts like this!

I ® @curlous_.progralllllNII

You might also like