Lab 9

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Lab 9 – Restricting Data Using Group By Clause

Exercise -1:
Write the answers of following questions in practical file
1 What is the difference between WHERE clause and HAVING clause?
ANS:-
WHERE Clause: Filters individual rows before grouping or aggregation. Used with non-aggregated columns
HAVING Clause: Filters grouped results based on aggregate functions. Used with aggregated columns in
combination with GROUP BY.

2 Why do we need to use the same columns that are selected in the SELECT list in the GROUP BY Clause?
ANS:-
Including the same columns in the SELECT list and the GROUP BY clause is necessary because the GROUP BY
clause defines how rows are grouped for aggregate functions, and including all non-aggregated columns in
the GROUP BY ensures clarity, consistency, and adherence to SQL standards

3 Why do we need to have columns that are not part of GROUP BY Clause as part of aggregate function?
ANS:-
When a column is not part of the GROUP BY clause, it must be included in an aggregate function to provide a
single value for each group, resolve ambiguity, ensure logical consistency, and comply with SQL standards.

4 Will fixing the error by adding the columns to Group By clause, result in correct output?
ANS:-
Adding columns to the GROUP BY clause resolves the syntax issue but doesn't guarantee correct results. To
ensure meaningful output, you should use appropriate aggregate functions for columns not included in the
GROUP BY clause.
5 In SQL, what’s the difference between the having clause and the group by statement?
ANS:-
GROUP BY Clause:
Purpose: Groups rows based on specified columns to perform aggregate calculations.
Usage: Appears in the SELECT statement followed by one or more columns.
Function: Defines the criteria for grouping rows, typically used with aggregate functions like COUNT, SUM, or
AVG.
HAVING Clause:
Purpose: Filters the grouped results based on conditions involving aggregate functions.
Usage: Appears after the GROUP BY clause and is followed by a condition.
Function: Applies conditions to aggregated values, allowing you to filter groups based on those conditions.
Exercise -2:
Solve the following queries on the basis of SMS database.
DEPARTMENT COURSE

SECTION
STUDENT

FACULTY

MINI PROJECT

RESEARCH PROJECT

STUDENT PHONE NO STUDENT HOBBY FACULTY COURSE

STUDENT COURSE FACULTY RESEARCH PROJECT


1. Write a query in SQL to find the number of faculty in each department along with the department id.
SELECT d.dept_id, COUNT(f.faculty_id) AS faculty_count FROM department d LEFT JOIN faculty f ON d.dept_id
= f.dept_id GROUP BY d.dept_id;

2. Write a query in SQL to find the sum of the allotment of salary amount of all departments.
SELECT SUM(salary) AS total_salary_allotment FROM faculty;

3. Write a SQL query to display the average salary amount in each department along with their id.
SELECT f.dept_id, AVG(f.salary) AS avg_salary FROM faculty f GROUP BY f.dept_id;

4. Write a SQL query to find the number of faculty getting salary more than or equal to Rs.60000.
SELECT COUNT(*) AS faculty_count FROM faculty WHERE salary >= 60000;

5. Write a SQL query to find the number of faculty teaching a course and number of students registered in that
course.
SELECT c.course_name, COUNT(DISTINCT f.faculty_id) AS faculty_count, COUNT(DISTINCT s.roll_no) AS
student_count FROM course c LEFT JOIN faculty_course fc ON c.course_id = fc.course_id LEFT JOIN faculty f ON
fc.faculty_id = f.faculty_id LEFT JOIN student_course sc ON c.course_id = sc.course_id LEFT JOIN student s ON
sc.roll_no = s.roll_no GROUP BY c.course_name;

6. Write a SQL query to find the number of faculty of each department who are working in any research project.
SELECT d.dept_id, COUNT(DISTINCT frp.faculty_id) AS faculty_count FROM department d LEFT JOIN
faculty_research_project frp ON d.dept_id =frp.faculty_id GROUP BY d.dept_id;

7. Write a SQL query to find the first name, faculty id and department of all faculty who are working in more than
two research projects.
SELECT f.first_name, f.faculty_id, f.dept_id FROM faculty f JOIN faculty_research_project frp ON f.faculty_id =
frp.faculty_id GROUP BY f.first_name, f.faculty_id, f.dept_id HAVING COUNT(DISTINCT frp.researchproj_id) >
2;

8. Write a SQL query to display the name and id of faculty who has maximum experience.
SELECT first_name, faculty_id FROM (SELECT first_name, faculty_id FROM faculty ORDER BY doj ASC) WHERE
ROWNUM = 1;
9. Write a SQL query to find the first name and department of all faculty who have completed any research project.
SELECT f.first_name, f.dept_id FROM faculty f JOIN faculty_research_project frp ON f.faculty_id =
frp.faculty_id;

10. Write a SQL query to find the student names and their departments who have registered in mini projects of
Artificial Intelligence domain.
SELECT s.first_name, s.roll_no, d.dept_name FROM student s JOIN mini_project mp ON s.roll_no = mp.roll_no
JOIN department d ON s.dept_id = d.dept_id WHERE mp.domain = 'Artificial Intelligence';

Bonus Exercise – 3:
Solve the following queries on the basis of schema given.
SCHEMA: S_ORD (CUS_ID, DATE_ORDER, DATE_SHIPMENT, SP_NO, TOTAL, PAYMENT)

1. Write SQL query that displays the total and average payments of all the credit orders.
SELECT SUM(TOTAL) AS TotalPayments, AVG(TOTAL) AS AveragePayments FROM S_ORD WHERE PAYMENT =
'CREDIT';

2. Write SQL query that displays the total and average payments grouped by type of payment.
SELECT PAYMENT,SUM(TOTAL) AS TotalPayments,AVG(TOTAL) AS AveragePayments FROM S_ORD GROUP BY
PAYMENT;

3. How many order dates are represented compared to the total number of orders?
SELECT COUNT(DISTINCT DATE_ORDER) AS NumberOfOrderDates,COUNT(*) AS TotalOrders FROM S_ORD;
4. How many customers and sales representative are represented compared to the total number of orders?
SELECT COUNT(DISTINCT CUS_ID) AS NumberOfCustomers,COUNT(DISTINCT SP_NO) AS
NumberOfSalesRepresentatives,COUNT(*) AS TotalOrders FROM S_ORD;

5. Write SQL query that displays the lowest and highest payments of all the orders.
SELECT MIN(TOTAL) AS LowestPayment,MAX(TOTAL) AS HighestPaymet FROM S_ORD;

6. What is the average amount of the order for each sales representative?
SELECT SP_NO,AVG(TOTAL) AS AverageOrderAmount FROM S_ORD GROUP BY SP_NO;

7. Write an SQL query to display the order dates and how many orders were on each date.
SELECT DATE_ORDER,COUNT(*) AS NumberOfOrders FROM S_ORD GROUP BY DATE_ORDER;

8. Write SQL query to display the order amount by payment type for each sales representative
SELECT SP_NO,PAYMENT,SUM(TOTAL) AS TotalOrderAmount FROM S_ORD GROUP BY SP_NO, PAYMENT;

9. Query to display the highest and lowest order for each order date where more than one order was placed.
SELECT DATE_ORDER,MAX(TOTAL) AS HighestOrder,MIN(TOTAL) AS LowestOrder FROM S_ORD GROUP BY
DATE_ORDER HAVING COUNT(*) > 1;

10. SQL query to display the average order for each order date where more than one order was placed and the average
order is greater than 1000. Display them in order of average order.
SELECT DATE_ORDER,AVG(TOTAL) AS AverageOrder FROM S_ORD GROUP BY DATE_ORDER HAVING COUNT(*) > 1
AND AVG(TOTAL) > 1000 ORDER BY AverageOrder;

11. Display the customer number with more than one order. Arrange alphabetically by customer id.
SELECT CUS_ID FROM S_ORD GROUP BY CUS_ID HAVING COUNT(*) > 1 ORDER BY CUS_ID;

You might also like