Joins Group by Aggregate Func
Joins Group by Aggregate Func
Joins Group by Aggregate Func
• 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;
• 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:
• Natural Join:
Natural join acts on those matching attributes where the values of
attributes in both the relations are same.
Left Join:
• LEFT JOIN returns only unmatched rows from the left table, as well as matched
rows in both tables.
Right Join:
• RIGHT JOIN returns only unmatched rows from the right table , as well as
matched rows in both tables.
• FULL OUTER JOIN returns unmatched rows from both tables,as well as matched
rows in both tables.
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.
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))
2) Write a SQL Query to find the name of publisher with second largest year of publication.
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:
2) Write a SQL query to count the total offline books available in library.
➔ Select count(*) from Offline_Books
SUM FUNCTION:
AVG FUNCTION:
➔ 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".
➔ Example Queries:
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.
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.