0% found this document useful (0 votes)
8 views8 pages

Revision Mysql Join

Uploaded by

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

Revision Mysql Join

Uploaded by

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

REVISION OF MYSQL JOIN

CLASS : XII
COMPUTER SCIENCE
JOIN

 The process/function of combining data from multiple tables is called a


JOIN.
 An SQL JOIN is used to combine rows from two or more tables, based
on a common field.

The types of SQL joins are as follows:


1. Cartesian Product (Cross Product)
2. Equi Join
3. Non-Equi Join
4. Natural Join
EXAMPLE:

Suppose the tables in database are:

CROSS JOIN / CARTESIAN PRODUCT

When we perform Cartesian product between two tables, all the rows in the
first table are joined to all the rows in the second table.
EQUI JOIN:

 The join in which columns are compared for equality is called Equi-
Join.
 In equi-join * is used in select command therefore the common column
will appear twice in the output.
EXAMPLE:

SELECT * FROM employee , dept


WHERE employee.deptno = dept.deptno;

Alias :
 We can also give table alias (short name) for table and further use them
in query in place of table name.
 This is helpful when table name is of big length & we can shorten the
query.
EXAMPLE:
SELECT * FROM employee e , dept d
WHERE e.deptno = d.deptno;

In the given SELECT statement, e and d are the alias names.


NATURAL JOIN

 The join in which only one of the identical columns exists is called
Natural Join.
 In natural join we specify the names of columns to fetch in such a way
so that identical columns appears only once.
EXAMPLE:
SELECT employee.* , dname, loc FROM employee , dept
WHERE employee.deptno = dept.deptno;

JOIN CLAUSE OF SQL SELECT


MySQL support various options with JOIN clause.
1. CROSS
2. NATURAL
3. ON
4. USING
CARTESIAN PRODUCT USING JOIN
EXAMPLE:
mysql>Select * from employee JOIN dept;
EQUI – JOIN USING JOIN

(a) WITH ON CLAUSE

EXAMPLE:

select * from employee JOIN dept ON employee.deptno = dept.deptno;

(b) WITH USING CLAUSE

EXAMPLE:

select * from employee JOIN dept USING (deptno) ;

NATURAL – JOIN USING JOIN

EXAMPLE:

select * from employee NATURAL JOIN dept ;


Consider the table employee…

QUESTIONS ON JOIN
1. Consider the following table GAMES. Write SQL commands for the
following statements.

(i) To display game type and the number of each type of game.
SELECT type , count(type) FROM Game GROUP BY type;
(ii) To display the maximum prize money in each types of game.
SELECT type , max(prizemoney) FROM Game
GROUP BY type;
(iii) To display sum of PrizeMoney for each Type of GAMES.
SELECT type , sum(prizemoney) FROM Game
GROUP BY type;
2. Consider the following table CABHUB. Write SQL commands for the
following statements:

(i) To find the number vehicle of each make in CUBHUB table.


SELECT make,COUNT(make) FROM CABHUB GROUP BY
make;

(ii) To display the highest charges in each type of Make at which a


vehicle can be hired from CABHUB.
SELECT MAX(charge) FROM cabhub GROUP BY make;

(iii) To display the minimum charges of capacity more than 5 in each


type of color.
SELECT color, MIN(charge) FROM cabhub GROUP BY color
HAVING capacity > 5;
3. Write the commands for the following query based on TEACHER Table:
GENDE
TNO TNAME Desig Age City R SALARY
T01 Rakesh Sharma PGT 45 DELHI MALE 127200
T02 Jugal Mittal TGT 39 DELHI MALE 104000
T03 Sharmila Kaur PGT 35 MEERUT FEMALE 152000
Sandeep
T04 Kaushik PRT 27 MUMBAI MALE 180000
T05 Sangeeta Vats TGT 24 MEERUT FEMALE 126700
T06 Ramesh Kumar PRT 39 MUMBAI MALE 188000
T07 Shyam Arora TGT 38 DELHI MALE 144000
T08 Shiv Om PGT 65 CHENNAI MALE 156000
T09 Vivek Rawat PRT 53 KOLKATA MALE 104000
T10 Rajest Verma PRT 51 MEERUT MALE 187200
T11 Ashok Singhal TGT 29 DELHI MALE 110000

I. To display the total salary for each designation.


SELECT desig, sum(salary) FROM teacher GROUP BY desig ;
II. To display the maximum salary for each designation.
SELECT desig, max(salary) FROM teacher GROUP BY desig;
III. To display the minimum salary for each designation.
SELECT desig, min(salary) FROM teacher GROUP BY desig;
IV. To display the total number of teacher in each designation.
SELECT desig,count(desig) FROM teacher GROUP BY
desig ;
V. To display the total number of teacher in each designation whose
age greater than 45;
SELECT desig, count(desig) FROM teacher
GROUP BY desig HAVING age > 45;
VI. To display the total number of teacher in each designation whose
age less than 45;
SELECT desig, sum(salary) FROM teacher
GROUP BY desig HAVING age < 45;
VII. To display the total number of female teacher in each
designation.
SELECT desig, count(gender) FROM teacher
GROUP BY desig , gender;

You might also like