Joins Group by Aggregate Func

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Practical-6

Objective: Implementation of Joins in SQL.


Joins: A JOIN clause is used to combine rows from two or more tables,
based on a related column between them.
Types of Joins:

• Inner Join
1) Equi Join
2) Theta Join
3) Natural Join
• Outer Join
1) Left Join
2) Right Join
3) Full outer Join

Inner Join:
The INNER JOIN keyword selects records that have matching values in
both tables.

• Equi join:
EQUI JOIN creates a JOIN for equality or matching column(s) values of the relative tables.
EQUI JOIN also create JOIN by using JOIN with ON and then providing the names of the
columns with their relative tables to check equality using equal sign (=).

->Syntax :
SELECT column_list
FROM table1 inner join table2
ON table1.column_name =
table2.column_name;

1) Select * from Books inner join


Online_Books ON ISBN = BookISBN
2) Select * from Readers inner join
Reports ON Readers.User_id = Reports.User_id

• Theta Join:
Theta join combines tuples from different relations provided they satisfy the theta
condition. The join condition is denoted by the symbol θ.

Notation: R1 ⋈θ R2:

1) Select * from Staff inner join


Login ON Staff.S_id = Login.S_id
inner join Authentication_System
ON Login.Login_id = Authentication_System.Login_id

• Natural Join:
Natural join acts on those matching attributes where the values of
attributes in both the relations are same.

Select * from Readers natural join Reports


Outer Join:
Outer joins are joins that return matched values and unmatched
values from either or both tables.

Left Join:

• LEFT JOIN returns only unmatched rows from the left table, as well as matched
rows in both tables.

Select * from Online_books left join

Books ON Online_Books.BookISBN = Books.ISBN

Select * from Offline_books


left join Books
ON Offline_books.BookISBN = Books.ISBN

Right Join:

• RIGHT JOIN returns only unmatched rows from the right table , as well as
matched rows in both tables.

Select * from Phones right join Readers ON Phones.User_id = Readers.User_id


Full Outer Join

• FULL OUTER JOIN returns unmatched rows from both tables,as well as matched
rows in both tables.

Select * from Books

full outer join Online_Books

ON Books.ISBN = Online_books.BookISBN
Practical- 7
Objective: Implementation of Aggregate functions in SQL
Aggregate Functions():
Functions that performs a calculation on a set of values, and returns a single
value are called aggregate functions.

The most commonly used SQL aggregate functions are:

• MIN() - returns the smallest value within the selected column


• MAX() - returns the largest value within the selected column
• COUNT() - returns the number of rows in a set
• SUM() - returns the total sum of a numerical column
• AVG() - returns the average value of a numerical column

SQL QUERIES:
MAX FUNCTION:

1) Write a SQL Query to find the second largest price of offline books with ISBN in
library.
➔ Select * from Offline_Books where price = (Select max(price) from Offline_Books
where price != (Select max(price) from Offline_Books))

Offline Books Table: Result:

2) Write a SQL Query to find the name of publisher with second largest year of publication.

➔ Select P_Name from Publisher where Year_of_Publish = (Select max(Year_of_Publish) from


Publisher where Year_of_publish != (Select max(Year_of_publish) from Publisher))
MIN FUNCTION:

1) Write a SQL query to find the book details with least ISBN number.
➔ Select * from Books where ISBN = (Select min(ISBN) from Books)

2) Write a SQL query to display readers details with second smallest user_id.
➔ Select * from Readers where user_id = (Select min(user_id) from Readers where user_id != (Select
min(user_id) from Readers))

COUNT FUNCTION:

1) Write a SQL query to count the number of readers.


➔ Select count(*) from Readers

2) Write a SQL query to count the total offline books available in library.
➔ Select count(*) from Offline_Books

SUM FUNCTION:

1) Write a SQL query to find total price of all offline_books.


➔ Select sum(price) from Offline_Books

AVG FUNCTION:

1) Write a SQL query to find the avg price of offline books


➔ Select avg(price) from Offline_Books
Practical- 8
Objective: Implementation of Order by and Group by commands
➔ Order by: The ORDER BY keyword is used to sort the result-set in ascending or
descending order.
➢ Syntax: SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

➔ Group by: The GROUP BY statement groups rows that have the same values into
summary rows, like "find the number of customers in each country".

The GROUP BY statement is often used with aggregate functions


(COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.

➢ Syntax: SELECT column_name(s)


FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);

➔ Example Queries:

1) Write a SQL query to display the staff name in descending order.


Select S_name from Staff order by S_name DESC

2) Write a SQL query to display category of books with count more than one.
SELECT category, COUNT(category)
FROM Books
GROUP BY category
HAVING COUNT(category) > 1;

3) Write a SQL query to display the P_name who publishes atleast one book with year of publication
2000 in chronological order only.
SELECT P_name, COUNT(Year_of_publish)
FROM Publisher
WHERE Year_of_publish > 2000
GROUP BY P_name order by P_name;
4) Write a SQL query to find the Publisher name who publishes books with ISBN more than 40000 in
descending order of names.

Select P_name from Publisher, Books, Publishes_Books

where ISBN > 40000

group by P_name order by P_name DESC

5) Write an SQL query to display the staff name who manages more than one reports of user/readers
uniquely identified by S_id in descending order.

Managed_By Table: Staff Table

SELECT S_name, COUNT(S_id)


FROM Managed_By NATURAL JOIN Staff
WHERE S_id > 1
GROUP BY S_name
ORDER BY S_name DESC;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy