0% found this document useful (0 votes)
9 views

DBMS

Uploaded by

ramnathjhrav
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)
9 views

DBMS

Uploaded by

ramnathjhrav
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

Expt.

No:
Page No.:
Date:

ADITYA ENGINEERING COLLEGE, SURAMPALEM


2 2 A 9 1 A 6 1
Expt. No:
Page No.:
Date:

ADITYA ENGINEERING COLLEGE, SURAMPALEM


2 2 A 9 1 A 6 1
Expt. No:
Page No.:
Date:
Experiment-5
5) Queries on joins and Corelated sub-queries.
Aim: To perform queries on joins and corelated sub-
queries. Description:
 Join is an operation in DBMS (Database Management System) that combines the row of two or
more tables based on related columns between them.
 Join is an operation in DBMS(Database Management System) that combines the row of two or
more tables based on related columns between them. The main purpose of Join is to retrieve
the data from multiple tables in other words Join is used to perform multi-table query.

Left Join:
 Left Join retrieves all the records and the data from the left table and all matching records from
the right table.
 Here, the “LEFT JOIN” keyword is used.
 In Left Join it returns rows from both tables and all the rows from the left table.

Syntax:
SELECT columns
FROM left_table
LEFT JOIN right_table ON
join_condition;
Right Join:
 Right Join retrieves all the records and the data from the right table and all matching records
from the left table.
 Here, the “RIGHT JOIN” keyword is used.
 In Right Join it returns rows from both tables and all rows from the right table.

Syntax:
SELECT columns
FROM left_table
RIGHT JOIN right_table ON
join_condition;

Inner Join/ Join:


 The INNER JOIN keyword selects records that have matching values in both tables.

ADITYA ENGINEERING COLLEGE, SURAMPALEM


2 2 A 9 1 A 6 1
Expt. No:
Page No.:
Date:

Syntax:
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

Full Join:
 The FULL OUTER JOIN keyword returns all records when there is a match in left (table1) or right
(table2) table records.
 FULL OUTER JOIN and FULL JOIN are the same.

Syntax:
SELECT column_name
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;

ADITYA ENGINEERING COLLEGE, SURAMPALEM


2 2 A 9 1 A 6 1
Expt. No:
Page No.:
Date:
Program:
 Let’s get data from two tables

 select d.dname, e.eid, e.ename, e.did from dept d inner join emps e on d.did=e.did;

 select d.dname, e,eid, e,ename, e.did from dept d left join emps e on d.did=e.did;

ADITYA ENGINEERING COLLEGE, SURAMPALEM


2 2 A 9 1 A 6 1
Expt. No:
Page No.:
Date:
 select d.dname, e.eid, e.ename, e.did from dept d right join emps e on d,did=e,did;

 select d.dname, e.eid, e.ename, e.did from dept d full join emps e on d.did=e.did;

Sub-Queries:
1. Scalar Subqueries:
 A scalar subquery returns a single value (one row and one column).
 Often used in the WHERE clause to filter results based on aggregated values.

Syntax:
SELECT column_name
FROM table_name
WHERE expression operator (SELECT single_column FROM subquery);

2. Multiple Row Subqueries:


 These subqueries return multiple rows.
 Used in scenarios where you need to compare data across rows.

ADITYA ENGINEERING COLLEGE, SURAMPALEM


2 2 A 9 1 A 6 1
Expt. No:
Page No.:
Date:
Syntax: SELECT column_name
FROM table_name
WHERE column_name IN (SELECT single_column FROM subquery);

3. Multiple Column Subqueries:


 Return multiple columns.
 Useful when you want to retrieve related data from different tables.

Syntax: SELECT column1, column2, ...


FROM table_name
WHERE (column1, column2, ...) operator (SELECT subquery_column1,subquery_column2,
... FROM subquery);

4. Correlated Subqueries:
 Reference columns from the outer query within the subquery.
 The subquery execution depends on the outer query.

Syntax: SELECT column1, column2, ...


FROM table1 outer
WHERE column1 operator (SELECT column1, column2 FROM table2 WHERE expr1 =
outer.expr2);

Program :
Lets get the data from tow tables namely employee,empid;

 Select * from employee;

ADITYA ENGINEERING COLLEGE, SURAMPALEM


2 2 A 9 1 A 6 1
Expt. No:
Page No.:
Date:
 select * from empid;

 select max(salary) from employee where salary<(select max(salary) from employee;

 Multiple row subquery

 Multiple column subquery

 Correlated subqueries

ADITYA ENGINEERING COLLEGE, SURAMPALEM


2 2 A 9 1 A 6 1

You might also like