Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 36

SQL QUERIES-8

Display Data from Multiple Tables:JOINS


Group by

• The SQL GROUP BY clause is used in collaboration with


the SELECT statement to arrange identical data into
groups. This GROUP BY clause follows the WHERE clause
in a SELECT statement
Syntax
• SELECT column1, column2 FROM table_name WHERE
[ conditions ] GROUP BY column1, column2
• SELECT NAME, SUM(SALARY) FROM CUSTOMERS GROUP
BY NAME;
Having clause

• The WHERE clause places conditions on the selected


columns, whereas the HAVING clause places conditions
on groups created by the GROUP BY clause.
• SELECT column1, column2 FROM table1, table2 WHERE
[ conditions ] GROUP BY column1, column2 HAVING
[ conditions ]
Example
E_id E_name Dept Salary
1 Ram HR 20000
2 Amit Finance 30000
3 Ravi HR 40000
4 Nitin IT 50000
5 Rahul Finance 60000

Condition for the group by:


1.Whatever attribute you write
with the group by, the same you
will write with select
2.If you want to use any other
attribute with select then apply an
aggregate function on that
Having clause

• SELECT id, SUM(salary) FROM employee GROUP BY id


having id>2;
Example
Write a query to display all E_id E_name Dept Salary
the department names with 1 Ram HR 20000
the number of employees 2 Amit Finance 30000
working in it. 3 Ravi HR 40000
select job from emp 4 Nitin IT 50000
group by dept; 5 Rahul Finance 60000

Condition for the group by:

select job, count(*) from 1.Whatever attribute you write


with the group by, the same you
emp group by job; will write with select
2.If you want to use any other
attribute with select then apply an
select job, count(job) from aggregate function on that
emp group by job;
JOINS
• SQL Join is used to fetch data from two or more tables, which
is joined to appear as single set of data.
• SQL Join is used for combining column from two or more
tables by using values common to both tables. 
• Join Keyword is used in SQL queries for joining two or more
tables. Minimum required condition for joining table, is
(n-1) where n, is number of tables.
• A table can also join to itself known as, Self Join.
• (INNER) JOIN: Returns records that have matching values
in both tables
• LEFT (OUTER) JOIN: Returns all records from the left
table, and the matched records from the right table
• RIGHT (OUTER) JOIN: Returns all records from the right
table, and the matched records from the left table
• FULL (OUTER) JOIN: Returns all records when there is a
match in either left or right table
JOINS
• This type of JOIN returns the cartesian product of
rows from the tables in Join.
• It will return a table which consists of records which
combines each row from the first table with each row
of the second table.
• Cross JOIN Syntax is:
• SELECT column-name-list from table-name1 CROSS
JOIN table-name2;
• SELECT * from class cross JOIN class_info;
Example of Cross JOIN

cust_details table
orders table
cid Cname addres oid oamount cid
s
1 Ram Delhi 1 55 1

2 Sham Chd 2 80 2

3 Ravi Chd 3 100 1

4 Amit Delhi 4 90 3
Inner Join
• Select records that has matching values in both tables
• Inner join will show common records

select * from cust_details, orders where cust_details.cid


= orders.cid
Using Join
select * from cust_details JOIN orders ON cust_details.cid
= orders.cid
Inner Join

Query:
Display cname, order amount of every customer (which
customer has placed order of what amount)?
select cid, cname, oamount from cust_details JOIN
orders ON cust_details.cid = orders.cid

Error: cid is ambigiuos


Inner Join
Query:
Display cname, order amount who has placed order of
what amount?
Sol:
select cust_details.cid, cname, oamount from
cust_details JOIN orders ON cust_details.cid = orders.cid
Left Outer Join
select cname, email from cust_details left join orders on
cust_details.cid = orders.cid

Query:
Find all the customers with their order details.
select cust_details.cid, cname, oamount from
cust_details LEFT JOIN orders ON cust_details.cid =
orders.cid
Right Outer Join
select cname, email from cust_details right join orders on
cust_details.cid = orders.cid

Query:
Find all the orders with their customer details.
select cust_details.cid, cname, oamount from
cust_details RIGHT JOIN orders ON cust_details.cid =
orders.cid
1.Cross JOIN or Cartesian Product
• This type of JOIN returns the cartesian product of
rows from the tables in Join.
• It will return a table which consists of records which
combines each row from the first table with each row
of the second table.
• Cross JOIN Syntax is:
• SELECT column-name-list from table-name1 CROSS
JOIN table-name2;
• SELECT * from class cross JOIN class_info;
Example of Cross JOIN

The Class table The class_info table

ID NAME ID Address

1 abhi 1 DELHI

2 adam 2 MUMBAI

4 alex 3 CHENNAI
Cross JOIN query will be,
•SELECT * from class cross JOIN class_info;
2.INNER Join or EQUI Join
• This is a simple JOIN in which the result is based on matched
data as per the equality condition specified in the query.
Inner Join Syntax is,
• SELECT <column-name-list> from <table-name1> INNER JOIN
<table-name2> WHERE <table-name1.column-name> =
<table-name2.column-name>;
• SELECT * from class, class_info where class.id = class_info.id;
• The result table will look like,

ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
3.NATURAL JOIN
• Natural Join is a type of Inner join which is based on
column having same name and same datatype
present in both the tables to be joined.
Natural Join Syntax is:
SELECT * from table-name1 NATURAL JOIN table-
name2;
• SELECT * from class NATURAL JOIN class_info;
Example of Natural JOIN

The class table, The class_info table,
ID NAME ID Address

1 abhi
1 DELHI

2 adam
2 MUMBAI
3 alex

3 CHENNAI
4 anu
• Natural join query will be,
• SELECT * from class NATURAL JOIN class_info;
• The result table will look like,
ID NAME Address
1 abhi DELHI
2 adam MUMBAI
3 alex CHENNAI

• In the above example, both the tables being joined have ID


column(same name and same datatype), hence the records
for which value of ID matches in both the tables will be the
result of Natural Join of these two tables.
4.Outer JOIN
• Outer Join is based on both matched and
unmatched data. Outer Joins subdivide further into,
• Left Outer Join
• Right Outer Join
• Full Outer Join
Left Outer Join
• The left outer join returns a result table with
the matched data of two tables then remaining rows
of the left table and null for the right table's column.
• Left outer Join Syntax for Oracle is,
• select column-name-list from table-name1 LEFT
OUTER JOIN on table-name1.column-name = table-
name2.column-name.
• SELECT * FROM class LEFT OUTER JOIN class_info ON
(class.id=class_info.id);
Right Outer Join
• The right outer join returns a result table with
the matched data of two tables then remaining
rows of the right table and null for the left table's
columns.
• Right outer Join Syntax for Oracle is,
• select column-name-list from table-name1, table-
name2 on table-name1.column-name= table-
name2.column-name;
• SELECT * FROM class RIGHT OUTER JOIN class_info
ON (class.id=class_info.id);
Full Outer Join

• The full outer join returns a result table with


the matched data of two table then remaining rows
of both lefttable and then the right table.
• Full Outer Join Syntax is,
• select column-name-list from table-name1 FULL
OUTER JOIN table-name2 on table-name1.column-
name = table-name2.column-name;
• SELECT * FROM class FULL OUTER JOIN class_info ON
(class.id=class_info.id);
• The SQL SELF JOIN is used to join a table to itself as
if the table were two tables.
• SELECT a.column_name, b.column_name FROM
table1 a, table1 b WHERE a.common_field =
b.common_field;
• SELECT a.ID, b.NAME, a.SALARY FROM CUSTOMERS
a, CUSTOMERS b WHERE a.SALARY < b.SALARY;
PRACTICE

• Consider the following database:


• Employee(emp_id,name,dept_id,job,email,mobile_
number,join_date,salary,mgr_id)
• Department(dept_id,dept_name,block_no);
• Query to display the name , department number and
department name for all employees.
• Query to display the employee name and department
name for all the employees who have an ‘a’ in there
name
• Query to display name,job,department number and
department name for all the employees who work in
block no 9.
• Query to display the employee name and department
number of all the employees who work in the same
department as any given employee
• Query to display the name and joining date for all
employees who have joined before managers along
with their manger number and joining date
• Query to display each department number ,number
of employees in the department and the average
salary for all the employees in that department.
• Query to display the name and joining date of all
employees who had joined after employee ‘RAJ’.
solution
• Select name,employee.dept_id,dept_name from
employee,department where
employee.dept_id=department.dept_id;
• 2.select name,dept_name from employee,department
where employee.dept_id=department.dept_id and
name like ‘%a%’;
• 3.select name,job,employee.dept_id,dept_name from
employee,department where
emplyee.dept_id=department.dept_id and block_no=9
• 4.select name,dept_id from employee where dept_id=(select
dept_id from emplyee where name=‘&name’);
• 5.select a.name,a.join_date,a.mgr_id,b.join_date from
employee a,employee b where a.mgr_id=b.emp_id;
• 6.select employee.dept_id,count(name),avg(salary) from
department,employee where
department.dept_id=employee.dept_id group by
employee.dept_id;
• Select name,joindate from emplyee where join date>(select
join date from employee where name=‘RAJ’);

You might also like