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

DBMS Lab 5 (Set Operations)

SQL queries of set Operations

Uploaded by

gamersop867
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

DBMS Lab 5 (Set Operations)

SQL queries of set Operations

Uploaded by

gamersop867
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

4.

SQL Queries on Set operations, Aggregate functions and Join Operations

● – The UNION command combines the result set of two


or more SELECT statements (only distinct values)
Q: To find the set of all courses taught either in Second 2022 or in First 2023, or both
(Select courseid
from section
where semester = ‘second’ and year= 2022)
union
(Select courseid
from section
where semester = ‘first’ and year= 2023);


● The UNION ALL command combines the result set of two or more SELECT
statements (allows duplicate values).
(Select courseid
from section
where semester = ‘second’ and year= 2022)
union all
(Select courseid
from section
where semester = ‘first’ and year= 2023);

To compare the rows of two or more SELECT statements, the INTERSECT operator is
used. After the comparing process, the INTERSECT operator returns the common or
intersecting records from the corresponding columns of the selected expressions.

Q: To find the set of all courses taught in the in Second 2022 as well as in First 2023

(Select courseid
from section
where semester = ’Second’ and year= 2022)
intersect
(Select courseid
from section
where semester = ’First’ and year= 2023);

The except operation outputs all tuples from its first input that do not occur in
the second input; that is, it performs set difference. The operation
automatically eliminates duplicates in the inputs before performing set
difference
Q: To find all courses taught in the Second 2022 semester but not in the first
2023 semester
(Select courseid
from section
where semester = ’Second’ and year= 2022)
except
(Select courseid
from section
where semester = ’First’ and year= 2023);

Aggregate functions are functions that take a collection (a set or multiset) of


Values as input and return a single value. SQL offers five built-in aggregate functions.
•Average: avg
•Minimum: min
•Maximum: max
•Total: sum
•Count: count

Q: To find the average salary of instructors and belongs to CSE Department.

Select avg (salary) as avg salary from


instructor
where dept_name= ’CSE.’;
Q: Find the total number of instructors who teach a course in the first 2023 semester.
Select count (distinct ID)
from teaches
where semester = ’First’ and year = 2023;
Grouping Aggregations:
Q: Find the average salary in each department.

Select dept_name, avg (salary) as avg salary


from instructor
group by dept_name;

Q: Find the departments where the average salary of the instructors is more than
25000.

Select dept_name, avg (salary) as Avg Salary


from instructor
group by dept_name
having avg (salary) > 25000;

Select Sum(salary) From


instructor;

Select MIN(Salary) From


instructor;

Select MIN(Salary) From


instructor
Where dept_name =’csa’

Select MIN(Salary) From


instructor;

Select MAX (Salary)


From instructor
Where dept_name =’csa’;

select count (*)


from course;

A JOIN clause is used to combine rows from two or more tables, based on a related
column between them.
Let's look at the “Orders" and “Customers” tables:

✔ Create Orders table with columns orderid, customerid, ordernumber and


Create customers table with columns customerid, customername, contactname
and country.
The INNER JOIN keyword returns only rows with a match in both
tables.

Q: selects records that have matching values in both tables.

SELECT Orders.orderid, Customers.customername,


Orders.ordernumber FROM Orders
INNER JOIN Customers ON orders.customerid=Customers.customerid;

The LEFT JOIN keyword returns all records from the left table (table1), and the
matching records from the right table (table2). The result is 0 records from the right
side, if there is no match.

Q: SELECT Customers.customername,
Orders.orderid FROM Customers
LEFT JOIN Orders ON Customers.customerid = Orders.customerid
ORDER BY Customers.customername;

– The RIGHT JOIN keyword returns all records from the right table
(table2), and the matching records from the left table (table1). The result is 0
records from the left side, if there is no match.

SELECT Orders.orderid, customers.customername,


FROM Orders
RIGHT JOIN Employees ON Orders.customerid =
customers.customerid ORDER BY Orders.orderid;

– The FULL OUTER JOIN keyword returns all records when there is
a match in left (table1) or right (table2) table records.

SELECT Customers.customername, Orders.orderid


FROM Customers
FULL OUTER JOIN Orders ON Customers.customerid=Orders.customerid
ORDER BY Customers.customername;

You might also like