SQL Introduction
Database:- It is an organized collection of data
so that it can be easily accesšed.
To manage these databases,DBMS ( DataBase
Management System) are used.
DBMS Data Base
Tybes of DBMS:-
@progsammer-girl.
Relational DBMS
Non- Relational DBS
Relational DBMS:- In this DBMS, data stored in
table Format.
Name cla88
RoliNo
Jai 5th
1
2 Amar 7th
5th
3 Anuj
4 Ram 8th
(Relational DBMS)
For Example :- MysaL, Oracle.
Non-Relational DBMSs:- In this DBMS data is
stored in key-value pais.
Σ
"RollNo": 1,
" class":"5th",
2
name". "Jai"
}
CNon-Relational DBMS)
For Example :- MongoDB, Reds
SQL:- I7 stand for structured Query language.
SQL is used for update, delete, insert data in
table or Relational DataBase.
SQL
RDBMS
Relational
DataBase
@programmer-girk-
Date:
Page No:
My Champion
SQL CREATE Command:-
It is used for creating Tables.
Syntax-
CREATE TABLE tableName(
Column1 datatype,
Column 2 datałybe,
زد
SAL Keywords are case-insensitive.
In MysQL, case-insensitive is an option you can
turn on and off.
For Examble:-
CREATE TABLE User (
First Name varchar,
Last Name varchar,
Email id varchar,
password varchar
زد
FisetName Last NamelEmail id pass OAd
PAAW
@programmer-girl-
SQL INSERT INTO Comman
d:-
data
It is used to insert into tables.
Syntax:
INSERT INTO table Name (colum
n1,
Column2 .... )
VALUES ( valuel, Valuez... );
A row of database table is known as record
or a tuple.
A column of database table is knowh as an
attribute.
@programmer-girta-
For Example :-
INSERT INTO USER (First Name,
LastName,Email-id, Password)
VALUES (Jai, Sharma, abc@[Link],
abc #123);
FisstName LastName Email-id Password
Jai Sharma Labc@gmaill abc #123
How to Insert Multiple Record (row, tuple):
VALUES (Jai, Sharma, aba@[Link], 123)
(Jaya,Sharma, xyz@[Link], abc);
Page Na :
-My Champion
SQL SELECT Command :-
It is used to retrieves data from the table
Syntax
SELECT Column1, Column2
From tableName;
To Select complete table, Use * (star)
SELECT*
From tableName;
Example:- @programmer-girl--
FirstName LastName Possword
Jai Kumar 123
Jaya Singh abc
Amit Sharma xyz
Table:- USER
Command:-
SELECT FirstName
From USER;
outbut:
First Name
Jai
Jaya
Amit
Page No:
My Champion
HOw DOES SQL
WORK
SQL Query
Query Language Processor
Parser+ optimizer
DBMS Engine
File Manager+
Transaction Ma
Manager
Physical Database
Parseng - In this process, Query statement is
tokenized.
optimising:- It optimize the best algorithm
for the byte code.
@programmer-girl
Page No
-My Champion
FROM: It is used to specify the tables from
Wh ich data fetched.
WHERE: It is used to filter records based on
the givan condition.
JOIN:- It is used to combine data from
tables based on a common field.
GROUP BY:- It is used to group records based
on our requirement.
HAVING :- It is used to filter groups.
ORDER BY:- It is used to sort the data in
ascending or descending order.
SELECT:- It is used to retrieves data from
the table.
LIMIT:- It is used to specify how many
rows are seturned.
@programmer ginl
SQU
Constraints
Constraints
CHECK UNIQUE DEFAULT INDEX
NOT NULL PRIMARY FOREIGN
KEY KEY
@programmer-girl-
These Constraints also
constraints.
known as Integrity
SQL Constraints:- Constraints are the rules
and restrictions applied on the data in
a table.
NOT NULL:- Value cannot be Null in a
column.
UNIQUE:- value cannot be same in a
column.
@programmer girl--
.
PRIMARY KEY:- Used to uniquely identify a
row.
FOREIGN KEY :- References a row in another
table.
CHECK:- Satisfies a specific condition
DEFAULT:- Set default value
CREATE INDEX:- used to speedup the read
process.
Date:
Page No:
My Champion
Sau Query Erxcution
Order
FROM
JOIN
WHERE
GROUP R
BY
HAVING
SELECT
ORDER BY
LIMIT
@programmer-gin..
Date:
Page No:
My Champion
Difforence Between ALTER
And UPDATE
ALTER UPDATE
It is a DDL. It is a DML
It is for
used It is used for
adding, deleting,and updating the data
modifying attributes in the existing table.
of the table.
Changes are made Changes are made
to the table to the data.
Structure.
It sets the specified
By default, all the value to the tuple
values in the tuple
are initialized as if update command
is used.
null if
the ALTER
Command is used.
@programmer-girl.
Page No:
My Champion
SOL SELECT DISTINCT :-
It is used to return only unique values from a
specified column in a table.
Syntax:-
SELECT DISTINCT cdumn-name
FROM [Link];
Examble;
Fisst Name Last Name Password
Jai Kumar 123
Jaya Singh 123
Amit Sharma xyz
Table:- USER
Command :-
SELECT Distinct Password
FROM USER;
Outbut:
Passhiord
123
xyz
@programmer girl
Page No:
My Champion
SQL_WHERE CLAUSE :
It is used to filter rows in a table based
on a specified condition.
Syntax:
SELECT column-name
FROM table-name
WHERE condition;
Example:-
FisstName Last Name
Age
Tai Kumar 19
20
Jaya Singh 21
Amit Sharma
Table:- USER
Command:-
SELECT First Name, Last Name
FROM USER
WHERE Age >20;
Oufput:- Last Name
FirstName
Amit Sharma
@programmer-girt
Date:
Page No:
-My Champion
SQLAND
The AND operator returns true if both
condition are true, and false otherwise.
Syntax WHERE condition1 AND condition 2;
SQLOR:
It returns true if either condition is
true, and false if both conditions are
false.
Syntax: WHERE Condition1 OR condition2;
SQL NOT:-
It returns the opposite of a condition.
Syntax:- WHERE NoT condition;
@programmer-girl--
AND, OR, NOT operators are usedto combine
Conditions in a where clause to create more
Complex Filtering conditions.
SQL ORDER BY:-
It is used to sort the result of a query
in ascending or descending order.
Syntax
SELECT column1, column2,
FROM table-name
ORDER By Column1 [ASCJIDESC],column2
CASCJEDESCJ,.
is
ASs : I Ia used to Bont the hesult in axcending ondes.
Order.
descending
@programmer gint
Examble:-
FirstName LastName
Age
Jai Kumar 49
Jaya Singh 20
Amit Sharma 21
Table :- user
Command:-
SELECT * FROM user
ORDER BY Age;
Outbut:
FirstName LastName Age
Jaya Singh 20
Amit Sharma 21
Jai Kumas 49
-My Champion
INSERT INTO :-
It is used to insert data into a table.
Syntax:
Insert into tablename (column1, column2,.)
Values ( value1, value 2,;
Note: There must be the same number of values
as the same number of columns specified.
Examble:-
FirstName Last Name Age
Jai Kumar 10
Jaya Singh 15
Amit Sharma 20
Table: user
Command:-
Insest into uses (FirstName,LastName, Age)
Values (abc, xy2, 25);
@programmer-gial
Dutbut:
FirstName LastName Age
Jai Kumar 10
Jaya Singh 15
Amit Sharma 20
abc 25
nyz
Page No:
My Champion
SQL NULL Values
It is used to represent missing or unknown
data.
Note:- Null is different from zero or empty string.
Insert Null value:-
INSERT INTO tablename( column1,column2,.)
VALUES Cvalue1, Null,..);
To Check for Null values:
IS NUП :- SELECT column1, Column2,...
From table-name
WHERE column2 Is NULL;
IS NOT NULL:-
SELECT column1, column2,...
FROM table-name
WHERE Column1 IS NOT NULL;
@programmer girl
Page No:
-My Champion
SQL UPDATE ;-
It is used to modify existing data in table.
Syntax:-
UPDATE table-name
SET Column1 = Valuel, column 2 = Value2
,...
WHERE some-column = Some-valuе?
SEI: T+ is used to specify the column and
values to update.
Example:-
First Name LastName
Age
Jai Kumar 10
15
Jaya Singh
Amit Sharma 20
Table:- Users
command :-
@programmer gink-
UPDATE USERS
SET age = age+1;
outbut:-
FirstName LastName
Age
Tai Kumar
Jaya Singh 16
Amit Sharma 21
Page No
-My Champion
SQL DELETE
It is used to remove existing record fram
a table in a SQL Database.
Syntax-
DELETE FROM tablename WHERE Condition;
Note:- This operation is not reversible, So be
careful when using DELETE Statements!
@programmer-girl
Date:
Page No:
My Champion
SQL Wildcards:-
Wildcards special characters used in
are
SQL 'LIKE' operator to Bearch for a specific
pattern in a column of a table.
The percent sign (%) represents
.
zero, one
or multiple characters.
The sign (-) represents one,
underscore
Single character.
SQL LIKE:-
It is used to search for a specific
pattern in a column of a table.
Syntax:
SELECT column1, column2,...
FROM table-name
WHERE Column-name LIKE pattern;
@[Link]
SQL IN:-
It is used to specify multiple values in a
WHERE clause for filtering data.
Syntax
SELECT column1, column2,...
FROM table-hame
WHERE Columnname IN Cvalue1,Value2);
SQL Between :-
It is used to filter data based on a
range of values in a wHERE clause.
Syntax
SELECT column1, column2,...
FROM table-hame
WHERE column-name Between
value1 AND value 2;
SQL Alias:- @programmer-girl..
It is used to give a temporary name to
a table or a column in a query.
Syntax-
SELECT column-name As alias-name
From table_name;
- My Champion
SQL UNION Operator :=
It is used to combine the result sets of
two or more SELECT statements into a
Single result set.
NOTE: It returns only distinct rows.
Syntax-
SELECT column-name
FROM table-name-1
UNION
SELECT column-name
FROM table-name-2;
Example:- SELECT A SELECTВ
1 3
2 4
Union
3
↑ 5
1
2
@programmer-gird
My Champion
SQL GROUP BY It is used to group rows that
have the same values into
Summary
rows, Like "Find the number of customers in
each city."
SQL HAVING i It is used to filter the results of
a GROUP BY' query Based on the
values of an aggregate function.
SQL EXISTS:- It is used to check if a subqueny
returns any rows.
It returns a boolean value.
SQL ALL:- It is used to compare a value with
the result of a subquery.
It returns true if the value is true for all elements.
SQLANY:- It is also a comparison operator.
It returns true if the value is true
for at least one element.
@progsammer-girl
Page Na:
My Champion
Types of Erior In Sal
Syntax Errors:- These occur when SQL statements
do not follow the correct syntax and structure
of the language.
Semantic Errors:- These occur when the SQL
statement is grammatically correct, But does not
produce the desired result due to incorrect Logic.
Constraint voilations:- These occur when the SQL
Violates One or more constraints on the database,
Datatype Errors: These occur when data is
inserted in a way that does not match the
expected data type.
For Fxample :- Ihsest a string into a numesic field.
Transaction Errors:- These occur when a
transaction fails locking.
due to problems with lo
@programmer girt
SQL JOINS
SQL JOINS:- These statements allow us to
access information from fwo or more tabler
at once.
They also keep our database normalized.
Tybes of Joins:-
INNER JOIN
LEFT JOIN
RIGHT JOIN
FULL JOIN
@programmer girl
Swipe..
INNER JOIN
INNER JOIN:- It returns dataset that hav
matching values in both tables.
-table1 tablez
SELECT Column-name
FROM table1
INNER JOIN table 2
ON [Link]-name = table 2. Column-name;
@programmer-girl.-
Swipe→...
LEFT JOIN
LEFT JOIN:- It returns all records from the
left table and matched records from the
right.
table 2
table1t
SELECT column-name
FROM table1
LEFT JOIN table 2.
ON [Link]-name = [Link]-name
@progsammer-girl-
Swibe...
RIGHT JOIN
RIGHT JOIN:- It returns all records from
the right table and the matched records
from the left.
fable1 table 2
SELECT Column-hame
FROM table1
RIGHT JOIN table 2
ON [Link]-name = table [Link]-name;
@programmer-girl..
Swibe →...
5/5
FULL JOIN
FULL JOIN:- It returns all records when
there is a match in either the left table
or right table.
tablei table 2
SELECT column-name
FROM table 1
Full OUTER JOIN table 2.
ON table 1. column-name = table 2. column-hame
WHERE Condition.
@programmer-girl--
+ Primary,Key and Foreign Key
- It is a unique value
in
ory log
a table.
that te wed ln. identilya
a row.
If you are thinking about unique constant.
then you must to know the differe nce
Unique constant can store null vatue also
Primary key Cannot store null value
SYntax @pregrammez-girl-
Create table tablename(
columni datatype
columnz datatype
Brimary kay(columnname)
A table can contain anly one primary Key
→ Fordgin key key which is used to.
It is a
link. two table tegether
→ Foreign key can have multifle null values.
yntax:- @[Link]
Create table tablename (
columnidatatype
column2 datatype
I column1,- J
Foreign key CColumm
References [primary key dable name]
ود
LET'S TAKE EXAMPLE:-
Student
ROlLND Name Address
Primary RAM Delhi
Mumbai
Key 2 Lalit
3 RAM. Delhi
Name and Address are not unique.
Here RollNo is unique.
st
is
we discus
and
sed hefor
not
e, Pnima
null
ry
vala e
key
unique Result table
Maths Science
10%0
English
80%6. 70%0
90%0 60%0 20°10.
70% 50% 21%%
Maths Science English
70%0
10%0 80%6
60%0 2010
90%0
70% 50% 21%
You want to check RollNO3 marks in
ma th s, into picture
Foreign key cametable.
Student tablet Resut
we we have to add primary Key
in Result table which act as
Foruign Key Foreigh
P
Mathe Science
10%% 80 0
English
70%
RolIND.
6010 2
90%0 20°%0
70% 30% 3
21%
* Commit and Rollback:-
2 commit and Rollback are transaction
Statements.
* Commit-
It ts used to stosingtransacti pizmanantly
changes on.
performed by a
Commit Syntax:-
→
COMMIT
@progranmezagist
* Rollback:
It is used for rever
saction.
tran
ting chan ges puformed
by
a
a
ck &y
RoblbaRoll nt ax
back;
?