0% found this document useful (0 votes)
25 views86 pages

Unit-2 (2)

Uploaded by

defana4720
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views86 pages

Unit-2 (2)

Uploaded by

defana4720
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 86

WHERE Clause

• SQL clause helps us to retrieve a set or bundles of


records from the table.

• SQL clause helps us to specify a condition on the


columns or the records of a table.
WHERE Clause (Cont…)
• Syntax of where clause with a select query to retrieve all
the column values for every record from a table:

SELECT * FROM TABLENAME WHERE CONDITION;

• Syntax of according to the requirement, we only want to


retrieve selective columns, then we will use below syntax:

SELECT COLUMNNAME1, COLUMNNAME2 FROM TABLENAME WHE


RE
CONDITION;
WHERE Clause (Cont…)
• Example:

• mysql> SELECT * FROM employees WHERE Salary > 50000;

• mysql> UPDATE employees SET Name = "Harshada Sharma"


WHERE
City = "Jaipur";

• mysql> DELETE FROM employees WHERE Date_of_Joining =


"2013-12-12";
Group By Clause
• The Group By clause is used to arrange similar kinds of
records into the groups in the Structured Query
Language.
• The Group by clause in the Structured Query Language
is used with Select Statement.
• The Group By clause is specially used with the
aggregate function, i.e., max (), min (), avg (), sum (),
count () to group the result based on one or more than
one column.
Group By Clause (Cont…)
• The syntax of Group By clause:
SELECT * FROM TABLENAME GROUP BY COLUMNNAME;

• The syntax of Group By clause with Aggregate Functions:


SELECT COLUMNNAME1, Aggregate_FUNCTION (COLUMNNAME)
FROM TABLENAME GROUP BY COLUMNNAME;

Example:
mysql> SELECT SUM (Salary) AS Salary, City FROM employee
s GROUP BY City;
Having Clause
• When we need to place any conditions on the table's
column, we use the WHERE clause in SQL.

• But if we want to use any condition on a column in


Group By clause at that time, we will use the HAVING
clause with the Group By clause for column conditions.
Having Clause (Cont…)
• Syntax:
TABLENAME GROUP BY COLUMNNAME HAVING CONDITI
ON;

Example:
• mysql> SELECT Name, Designation, SUM (Salary) AS S
alary FROM employees GROUP BY City HAVING SUM (
Salary) > 45000;
ORDER BY CLAUSE

• Whenever we want to sort anything in SQL, we use the


ORDER BY clause.

• ORDER BY CLAUSE sorts the data in ascending or


descending order as per our requirement.
ORDER BY CLAUSE (Cont…)
• Syntax of ORDER BY clause to sort in ascending order:
SELECT COLUMN_NAME1, COLUMN_NAME2 FROM TABLE
_NAME ORDER BY COLUMN_NAME ASC;

• Example:
mysql> SELECT Name, Salary FROM employees ORDE
R BY Salary ASC;
ORDER BY CLAUSE (Cont…)
• Syntax of ORDER BY clause to sort in descending order:
SELECT COLUMN_NAME1, COLUMN_NAME2 FROM TABLE
_NAME ORDER BY COLUMN_NAME DESC;

• Example:
mysql> SELECT * FROM employees ORDER BY Name
DESC;
SQL Joins

• As the name shows, JOIN means to combine something.


In case of SQL, JOIN means "to combine two or more
tables".

• The SQL JOIN clause takes records from two or more


tables in a database and combines it together.
Inner Join
• Returns rows when there is a match in both the tables.
1.Staff table

ID Staff_NAME Staff_AGE STAFF_ADDRESS Monthley_Package


1 ARYAN 22 MUMBAI 18000
2 SUSHIL 32 DELHI 20000
3 MONTY 25 MOHALI 22000
4 AMIT 20 ALLAHABAD 12000
2.Payment table

Payment_ID DATE Staff_ID AMOUNT


101 30/12/200 1 3000.00
9
102 22/02/201 3 2500.00
0
103 23/02/201 4 3500.00
0
Inner Join
SELECT Staff_ID, Staff_NAME, Staff_AGE, AMOUNT
FROM STAFF s, PAYMENT p
WHERE s.ID =p.STAFF_ID;
This will produce the result like this:

STAFF_ID NAME Staff_AGE AMOUNT


3 MONTY 25 2500
1 ARYAN 22 3000
4 AMIT 25 3500
1 ARYAN 22 3000
Left Join
• Returns all the rows from left table combine with
matching rows of right table.

• If you get no matching in right table it returns NULL


values.
Syntax:
SELECT TableName1.columnName1, TableName2.column
Name2
FROM TableName1
LEFT JOIN TableName2
ON TableName1.ColumnName = TableName2.ColumnNa
me;
Table 1: employee

EmployeeID Employee_Name Employee_Salary

1 Arun Tiwari 50000


2 Sachin Rathi 64000
3 Harshal Pathak 48000
4 Arjun Kuwar 46000
5 Sarthak Gada 62000
6 Saurabh Sheik 53000
7 Shubham Singh 29000
8 Shivam Dixit 54000
9 Vicky Gujral 39000
10 Vijay Bose 28000
Table 2: Department

DepartmentID Department_Name Employee_ID


1 Production 1
2 Sales 3
3 Marketing 4
4 Accounts 5
5 Development 7
6 HR 9
7 Sales 10
Left Join

mysql> SELECT e.EmployeeID, e.Employee_Name,


e.Employee_Salary, d.DepartmentID, d.Department_Name

FROM employee e LEFT JOIN department d

ON e.EmployeeID = d.Employee_ID;
Result of Left Join

EmployeeID Employee_Name Employee_Salary DepartmentID Department_Name

1 Arun Tiwari 50000 1 Production

2 Sachin Rathi 64000 NULL NULL

3 Harshal Pathak 48000 2 Sales

4 Arjun Kuwar 46000 3 Marketing

5 Sarthak Gada 62000 4 Accounts

6 Saurabh Sheik 53000 NULL NULL

7 Shubham Singh 29000 5 Development

8 Shivam Dixit 54000 NULL NULL

9 Vicky Gujral 39000 6 HR

10 Vijay Bose 28000 7 Sales


SQL RIGHT JOIN

• Returns all the rows from right table combine with


matching rows of right table.

• If you get no matching in left table it returns NULL


values.
Syntax of Right Join
SELECT TableName1.columnName1, TableName2.column
Name2
FROM TableName1
RIGHT JOIN TableName2
ON TableName1.ColumnName = TableName2.ColumnNa
me;
Table 1: employee
EmployeeID Employee_Name Employee_Salary

1 Arun Tiwari 50000

2 Sachin Rathi 64000

3 Harshal Pathak 48000

4 Arjun Kuwar 46000

5 Sarthak Gada 62000

6 Saurabh Sheik 53000

7 Shubham Singh 29000

8 Shivam Dixit 54000

9 Vicky Gujral 39000

10 Vijay Bose 28000


Table 2: Department

DepartmentID Department_Name Employee_ID


1 Production 1
2 Sales 3
3 Marketing 4
4 Accounts 5
5 Development 7
6 HR 9
7 Sales 10
Right Join
mysql> SELECT e.EmployeeID, e.Employee_Name, e.Em
ployee_Salary,
d.DepartmentID, d.Department_Name
FROM employee e RIGHT JOIN department d
ON e.EmployeeID = d.Employee_ID;
Result of Right Join

EmployeeID Employee_Name Employee_Salary DepartmentID Department_Name


1 Arun Tiwari 50000 1 Production
3 Harshal 48000 2 Sales
Pathak
4 Arjun Kuwar 46000 3 Marketing
5 Sarthak Gada 62000 4 Accounts
7 Shubham 29000 5 Development
Singh
9 Vicky Gujral 39000 6 HR
10 Vijay Bose 28000 7 Sales
Full Outer Join
• SQL full outer join is used to combine the result of both
left and right outer join and returns all rows (don't care
its matched or unmatched) from the both participating
tables.
• Syntax for full outer join:
SELECT *
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name;
Full Outer Join(Cont…)
• Example:
SELECT * FROM Data_1
FULL OUTER JOIN Data_2
ON Data_1.Roll_no = Data_2.Roll_no;
Data 1
Roll_No Name

1 Rahul

2 Kunal

3 Jay

4 Vinay

5 Preeti
Data 2
Roll_no Address

1 Mumbai

2 Pune

3 Nasik

7 Bangalore

8 Goa
Resulting Table
Roll_no Name Roll_no Address
1 Rahul 1 Mumbai
2 Kunal 2 Pune
3 Jay 3 Nasik
4 Vinay NULL NULL
5 Preeeti NULL NULL
NULL NULL 7 Bangalore
NULL NULL 8 Goa
Cross Join
• Join operation in SQL is used to combine multiple tables
together into a single table.
• If we use the cross join to combine two different tables,
then we will get the Cartesian product of the sets of rows
from the joined table. When each row of the first table is
combined with each row from the second table, it is
known as Cartesian join or cross join.
• After performing the cross join operation, the total
number of rows present in the final table will be equal to
the product of the number of rows present in table 1 and
the number of rows present in table 2.
• SELECT TableName1.columnName1, TableName2.colum
nName2 FROM TableName1 CROSS JOIN TableName2 O
N TableName1.ColumnName = TableName2.ColumnNa
me;
• SELECT *FROM customer CROSS JOIN orders;

• Example:
mysql> SELECT *FROM customer CROSS JOIN orders;
Customer

Customer Name Age Salary


_ID
1 Aryan Jain 51 56000

2 Arohi Dixit 21 25000

3 Vineet Garg 24 31000


Orders

Order_ID Order_Date Cutomer_ID Amount

1 2012-01- 2 3000
20
2 2012-05- 2 2000
18
3 2012-06- 3 4000
28
Resulting Table
Customer_I Name Age Salary Order_ID Order_Date Customer_I Amount
D D
1 Aryan Jain 51 56000 1 2012-01- 2 3000
20
2 Arohi Dixit 21 25000 1 2012-01- 2 3000
20
3 Vineet 24 31000 1 2012-01- 2 3000
Garg 20
1 Aryan Jain 51 56000 2 2012-05- 2 2000
18
2 Arohi Dixit 21 25000 2 2012-05- 2 2000
18
3 Vineet 24 31000 2 2012-05- 2 2000
Garg 18
1 Aryan Jain 51 56000 3 2012-06- 3 4000
28
2 Arohi Dixit 21 25000 3 2012-06- 3 4000
28
SQL Aggregate Functions
• SQL aggregation function is used to perform the
calculations on multiple rows of a single column of a
table. It returns a single value.
• It is also used to summarize the data.
• Count
• Sum
• Avg
• Max
• Min
COUNT FUNCTION

• COUNT function is used to Count the number of rows in


a database table. It can work on both numeric and non-
numeric data types.

• Syntax:
COUNT(*)
or
COUNT( [ALL|DISTINCT] expression )
Product
PRODUCT COMPANY QTY RATE COST
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item7 Com1 5 30 150
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Item10 Com3 4 30 120
Count Function (Cont…)
• Example: COUNT()
SELECT COUNT(*)
FROM PRODUCT;
Output: 10
• Example: COUNT with WHERE
SELECT COUNT(*)
FROM PRODUCT_MAST;
WHERE RATE>=20;
Output: 7
Count Function (Cont…)
• Example: COUNT() with DISTINCT
SELECT COUNT(DISTINCT COMPANY)
FROM PRODUCT_MAST;
Ouput:3
SUM Function
Sum function is used to calculate the sum of all selected
columns. It works on numeric fields only.

Syntax:
SUM()
or
SUM( [ALL|DISTINCT] expression )
SUM Function (Cont…)
• Example: SUM()
SELECT SUM(COST)
FROM PRODUCT;
Output: 670

• Example: SUM() with WHERE


SELECT SUM(COST)
FROM PRODUCT_MAST
WHERE QTY>3;
Ouput: 320
AVG function

• The AVG function is used to calculate the average value


of the numeric type. AVG function returns the average
of all non-Null values.

• Syntax:
AVG()
or
AVG( [ALL|DISTINCT] expression )
AVG function (Cont…)

• Example:
SELECT AVG(COST)
FROM PRODUCT;
• Output:
67
MAX Function

• MAX function is used to find the maximum value of a


certain column. This function determines the largest
value of all selected values of a column.
• Syntax
MAX()
or
MAX( [ALL|DISTINCT] expression )
MAX Function (Cont…)

• Example:

SELECT MAX(RATE)
FROM PRODUCT;

Output:
30
Min Function
• MIN function is used to find the minimum value of a
certain column. This function determines the smallest
value of all selected values of a column.
• Syntax
MIN()
or
MIN( [ALL|DISTINCT] expression )
Min Function (Cont…)

• Example:

SELECT MIN(RATE)
FROM PRODUCT;
• Ouput:
10
SQL Comparison Operators
The SQL Operators which compare the values of two
columns in the database tables are called as comparison
operators.
SQL Equal Operator (=)

• This type of comparison operator selects only those


data from the table which matches the specified value.
• Syntax:
SELECT * FROM Table_Name WHERE Column_Name = V
alue;
Example:
SELECT * FROM Employee WHERE Emp_Salary = 35000
;
Employee
Employee_Id Emp_Name Emp_City Emp_Salary Emp_Bonus

101 Anuj Ghaziabad 35000 2000

102 Tushar Lucknow 29000 3000

103 Vivek Kolkata 35000 2500

104 Shivam Goa 22000 3000


Output
Employee_Id Emp_Name Emp_City Emp_Salary Emp_Bonus

101 Anuj Ghaziabad 35000 2000

103 Vivek Kolkata 35000 2500


SQL NOT Equal Operator (!=)

• This type of comparison operator selects only those


data from the table which does not match with the
specified value.

• Syntax:
SELECT * FROM Table_Name WHERE Column_Name !
= Value;

Example:
SELECT * FROM Cars WHERE Car_Price != 900000;
Cars
Car_Number Car_Name Car_Amount Car_Price

2578 Creta 3 900000

9258 Audi 2 1100000

8233 Venue 6 900000

6214 Nexon 7 1000000


Output:

Car_Number Car_Name Car_Amount Car_Price

9258 Audi 2 1100000

6214 Nexon 7 1000000


SQL Greater Than Operator (>)

• This type of comparison operator selects, modifies, and


deletes only those data from the table which are greater
than the value specified in the query.
• Syntax:
SELECT * FROM Table_Name WHERE Column_Name > V
alue;

Example:
SELECT * FROM Cars_Details WHERE Car_Number > 60
00;
Output
Car_Number Car_Name Car_Amount Car_Price

9258 Audi 2 1100000

8233 Venue 6 900000

6214 Nexon 7 100000


SQL Greater Than Equals to Operator (>=)

• This type of comparison operator retrieves, modifies, and deletes


only those data from the table which are greater than and equal to
the given value.

• Syntax:
SELECT * FROM Table_Name WHERE Column_Name > =Value;

• Example:
SELECT * FROM Student_Details WHERE Student_Total_Marks>=1
20;
Student_Details

Student_Id Student_Na Student_Ma Student_En Student_Tot


me ths glish al_Marks
201 Anuj 30 60 90
202 Tushar 25 100 125
203 Vivek 30 90 120
204 Shivam 40 80 120
Output
Student_Id Student_Name Student Student Student_Total
_Maths _English _Marks

202 Tushar 25 100 125

203 Vivek 30 90 120

204 Shivam 40 80 120


SQL Less Than Operator (<)

• This type of comparison operator in SQL selects only


those data from the table which are less than the given
value.

• Syntax:
SELECT * FROM Table_Name WHERE Column_Name < V
alue;

• Example:
SELECT * FROM Car_Details WHERE Car_Amount < 6;
Car_Details
Car_Number Car_Name Car_Amount Car_Price

2578 Creta 3 900000

9258 Audi 2 1100000

8233 Venue 6 900000

6214 Nexon 7 1000000


Output:
Car_Number Car_Name Car_Amount Car_Price

2578 Creta 3 900000

9258 Audi 2 1100000


SQL Less Than Equals to Operator (<=)

• This type of comparison operator selects only those


data from the table which are less than and equal to the
given value.
• Syntax:
SELECT * FROM Table_Name WHERE Column_Name <=
Value;

• Example:
SELECT * FROM Car_Details
WHERE Car_Amount <= 3;
Output:
Car_Number Car_Name Car_Amount Car_Price

2578 Creta 3 900000

9258 Audi 2 1100000


Between Operator
• The BETWEEN keyword is an operator in Structured Query
Language. It allows the database users to access the
values within the specified range.
• Syntax:
SELECT*FROM Table_Name WHERE Column_Name BETWE
EN Value_1 AND Value_2;

• Example:
SELECT * FROM Faculty_Info WHERE Faculty_salary BETW
EEN 25000 AND 40000;
Faculty_Info
Faculty_Id Faculty_Fir Faculty_Las Faculty_De Faculty_Sal
st_Name t_Name pt_Id ary
1001 Arush Sharma 4001 20000
1002 Bulbul Roy 4002 38000
1004 Saurabh Roy 4001 45000
1005 Shivani Singhania 4001 42000
1006 Avinash Sharma 4002 28000
1007 Shyam Besas 4003 35000
Output

Faculty_Id Faculty_Fir Faculty_Las Faculty_De Faculty_Sal


st_Name t_Name pt_Id ary

1002 Bulbul Roy 4002 38000

1006 Avinash Sharma 4002 28000

1007 Shyam Besas 4003 35000


Use NOT BETWEEN operator

• The NOT BETWEEN keyword is an operator in Structured


Query Language. It allows the database users to access
the values not within the specified range.
• Syntax:
SELECT*FROM Table_Name WHERE Column_Name NOT
BETWEEN Value_1 AND Value_2;

• Example:
SELECT * FROM Faculty_Info WHERE Faculty_salary
NOT BETWEEN 25000 AND 40000;
Output
Faculty_Id Faculty_Fir Faculty_Las Faculty_De Faculty_Sal
st_Name t_Name pt_Id ary
1001 Arush Sharma 4001 20000
1004 Saurabh Roy 4001 45000
1005 Shivani Singhania 4001 42000
IN Operator
• The WHERE clause with IN operator shows those records
in the result which are matched with the given set of
values.
• Syntax:
SELECT*FROM Table_Name WHERE Column_Name IN (
Value_1 ,Value_2);

• Example:
SELECT * FROM Faculty_Info WHERE Faculty_Dept_Id
IN (4001,4002);
Output
Faculty_Id Faculty_Fir Faculty_Las Faculty_De Faculty_Sal
st_Name t_Name pt_Id ary
1001 Arush Sharma 4001 20000
1002 Bulbul Roy 4002 38000
1004 Saurabh Roy 4001 45000
1005 Shivani Singhania 4001 42000
1006 Avinash Sharma 4002 28000
LIKE Operator
• LIKE clause searches for a match between the patterns
in a query with the pattern in the values present in an
SQL table. If the match is successful, then that
particular value will be retrieved from the SQL table.
• Syntax:
SELECT ColumnName1, ColumnName2 FROM TableNam
e WHERE ColumnName LIKE [Expression];
Employee_Details
ID Name City Salary Age

1 Priyanka Nasik 26000 20


Bagul
2 Riya Mumbai 72000 28
Sharma

3 Neha Varanasi 37000 19


Verma
4 Neeta Nasik 39500 21
Desai
5 Priya Udaipur 60000 32
Wagh
LIKE Operator
• Example:
SELECT * FROM employee_details WHERE Name LIKE '
Pr%';
Output:
ID Name City Salary Age

1 Priyanka Nasik 26000 20


Bagul

5 Priya Udaipur 60000 32


Wagh
LIKE Operator
• Example:
SELECT * FROM employee_details WHERE Name LIKE '%
ya%';
Output:
ID Name City Salary Age

1 Priyanka Nasik 26000 20


Bagul
2 Riya Mumbai 72000 28
Sharma
5 Priya Udaipur 60000 32
Wagh
LIKE Operator
• Example
SELECT * FROM employee_details WHERE City LIKE '%i'
;
Output:
ID Name City Salary Age

2 Riya Mumba 72000 28


Sharm i
a
3 Neha Varana 37000 19
Verma si
LIKE Operator
• Example:
SELECT * FROM employee_details WHERE Age LIKE '2%
';
Output:
ID Name City Salary Age

1 Priyanka Nasik 26000 20


Bagul
2 Riya Mumbai 72000 28
Sharma
4 Neeta Nasik 39500 21
Desai
LIKE Operator
• Example:
SELECT * FROM employee_details WHERE City LIKE 'Na
_ik';
Output:
ID Name City Salary Age

1 Priyanka Nasik 26000 20


Bagul
4 Neeta Nasik 39500 21
Desai
LIKE Operator
• Example:
SELECT * FROM employee_details WHERE Name LIKE ‘
_____a%';
Output:
ID Name City Salary Age

1 Priyanka Nasik 26000 20


Bagul
4 Neeta Nasik 39500 21
Desai
5 Priya Udaipur 60000 32
Wagh
LIKE Operator
• Example:
SELECT * FROM employee_details WHERE Name NOT LI
KE
'Priya%';
Output:
ID Name City Salary Age

2 Riya Mumbai 72000 28


Sharma
3 Neha Varanasi 37000 19
Verma
4 Neeta Nasik 39500 21
Desai

You might also like