Unit-2 (2)
Unit-2 (2)
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.
Example:
• mysql> SELECT Name, Designation, SUM (Salary) AS S
alary FROM employees GROUP BY City HAVING SUM (
Salary) > 45000;
ORDER BY CLAUSE
• 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
ON e.EmployeeID = d.Employee_ID;
Result of Left Join
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
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
• 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
• Syntax:
AVG()
or
AVG( [ALL|DISTINCT] expression )
AVG function (Cont…)
• Example:
SELECT AVG(COST)
FROM PRODUCT;
• Output:
67
MAX Function
• 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 (=)
• 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
Example:
SELECT * FROM Cars_Details WHERE Car_Number > 60
00;
Output
Car_Number Car_Name Car_Amount Car_Price
• Syntax:
SELECT * FROM Table_Name WHERE Column_Name > =Value;
• Example:
SELECT * FROM Student_Details WHERE Student_Total_Marks>=1
20;
Student_Details
• 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
• Example:
SELECT * FROM Car_Details
WHERE Car_Amount <= 3;
Output:
Car_Number Car_Name Car_Amount Car_Price
• 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
• 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