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

MYSQL Questions for Group by Clause

1. What is the purpose of GROUP BY clause in MySQL? How is it different from ORDER BY clause?

2. State difference between date functions NOW() and SYSDATE() of MySql.

3. State the difference between Where Clause and having clause?

3. Shanya Khanna is using a table EMPLOYEE. It has the following columns:


Admno, Name, Agg, Stream [column Agg contains Aggregate marks]
She wants to display highest Agg obtained in each Stream.
She wrote the following statement:
SELECT Stream, MAX(Agg) FROM EMPLOYEE;
But she did not get the desired result. Rewrite the above query with necessary changes to help her get the desired
output.

Question 1.
Consider the table PERSONS given below. Write commands in SQL for (i) to (iv) and write output for
(v) to (viii).
TABLE PERSONS

(i) Display the SurName, FirstName and City of people residing in Udhamwara city.
(ii) Display the Person IDs (PID), Cities and Pincode of persons in descending order of Pincode.
(iii) Display the FirstName and City of all the females getting Basic salaries above 40000.
(iv) Display FirstName and BasicSalaries of all the persons whose first name start with „G‟.

(v) SELECT SurName FROM PERSONS WHERE BasicSalary>= 50000;


(vi) SELECT SUM (BasicSalary) FROM PERSONS WHERE Gender = ‘F’;
(vii) SELECT Gender, MIN (BasicSalary) FROM PERSONS GROUP BY Gender;
(viii) SELECT Gender, COUNT (*) FROM PERSONS GROUP BY Gender;

Question2.
Consider the table DOCTOR given below. Write commands in SQL for (i) to (iv) and output for (v)
to (vii).
TABLE DOCTOR
(i) Display the names and dates of joining of doctors of Oncology department.
(ii) Display the names and salaries of doctors in descending order of salaries.
(iii) Display the names and salaries of all the female‟s doctors who are getting salary above $
50000.
(iv) Display names of each department along with total salary being given to doctors of that
department.

(v) SELECT Department FROM DOCTOR WHERE Salary >= 55000;


(vi) SELECT SUM (Salary) FROM DOCTOR WHERE Department = ‘Surgery’;
(vii) SELECT Doc_Name FROM DOCTOR WHERE Doc_Name LIKE ’J ’%;

Question 3
Consider the table LIBRARY given below. Write commands in MySQL for (i) to (iv) and output for
(v) to (vii).
TABLE LIBRARY

(i) To list the BookTitle of FND type.


(ii) To display a report listing BookTitle, Type and Price in descending order of price.
(iii) To count the number of BookTitle, Who have FND type.
(iv) To insert a new row in the table LIBRARY.
7, „Windows 8 Basics‟, „FND‟,‟McGraw‟, 7,150.

(v) SELECT AVG(Price) FROM LIBRARY WHERE Type = ‘FND’;


(vi) SELECT COUNT(DISTINCT Type) FROM LIBRARY;

Question4.
Consider the table STUDENT given below. Write commands in MySQL for (i) to (iv) and output for
(v) to (vii). (HOTS)
TABLE STUDENT
(i) To list the StudentName of Science Stream.
(ii) To display a report listing StudentName, Class and Stream in ascending order of
StudentName.
(iii) To count the number of students to Science Stream.
(iv) To insert a new row in the table STUDENT. (7, „Simran Kaur‟,‟X!l‟, „Commerce‟, 86, „A2‟)

(v) SELECT AVG(AggMarks) FROM STUDENT WHERE Stream=‘Science’;


(vi) SELECT COUNT (DISTINCT Stream) FROM STUDENT;
(vii) SELECT MIN(AggMarks) FROM STUDENT;

Question 5
Consider the table PRODUCT given below. Write commands in MySQL for (i) to (iv) and output for
(v) to (vii).
TABLE PRODUCT

(i) To list the ProductName where BrandName is Maharaja.


(ii) To display a report ProductName and BrandName and Price in descending order of price.
(iii) To count the number of ProductName.
(iv) To insert a new row in the table PRODUCT.
6, „Gas Stove‟,‟Maharaja‟,7, 6150

(v) SELECT AVG(Price) FROM PRODUCT WHERE BrandName = ‘Maharaja’;


(vi) SELECT COUNT (DISTINCT BrandName) FROM PRODUCT;
(vii) SELECT MAX(Price) FROM PRODUCT;
Question 6
Consider the table SPORTS given below. Write commands in MySQL for (i) to (iv) and output for (v) to (vii).
TABLE SPORTS
(i) To list the Name of the students whose Game is Cricket.
(ii) To display a report listing Name, Game and Grade in ascending order of name.
(iii) To count the number of students.
(iv) To insert a new row in the table SPORTS.
7, ‘Rishabh’, 9, ‘Cricket’, ‘A’

(v) SELECT Name, Game FROM SPORTS WHERE Name LIKE ‘k%‘,
(vi) SELECT COUNT (DISTINCT Game) FROM SPORTS;
(vii) SELECT Name, Game, Grade FROM SPORTS WHERE Grade = *B';

Question7.
Consider the table INTERIORS given below. Write commands in MySQL for (i) to (iv) and output
for (v) to (vii).
TABLE INTERIORS

(i) To list the ItemName whose Type is Office Table.


(ii) To display a report listing ItemName, Type and Price in descending order of Price.
(iii) To count the number of ItemName.
(iv) To insert a new row in the table INTERIORS.
7, „Comfort Zone‟,‟Double Bed‟,‟23/03/2011′, 15000

(v) SELECT ItemName, Type FROM INTERIORS WHERE ItemName LIKE ‘R%’;
(vi) SELECT COUNT (DISTINCT Type) FROM INTERIORS;
(vii) SELECT ItemName, Type FROM INTERIORS WHERE DateofStock>‘15/ll/2010’;

Question 8
Consider the table STUDENTS given below. Write commands in MySQL for (i) to (iv) and output
for (v) to (vii).
TABLE STUDENTS
(i) To list the StudentName whose Class is IX.
(ii) To display a report listing StudentName, Class and Score in descending order of Score.
(iii) To count the number of Students.
(iv) To insert a new row in the table STUDENTS.
7, „Divya Prakash‟, „Science‟, 355, „C‟;

(v) SELECT StudentName, Stream FROM STUDENTS WHERE Score >380;


(vi) SELECT COUNT (DISTINCT Stream) FROM STUDENTS;
(vii) SELECT StudentName, Class FROM STUDENTS WHERE Grade = ‘A’;

Question 9

Consider the following table FITNESS with details about fitness products being sold in the store.
Write command of SQL for (i) to (iv) and output for (v). (HOTS)
TABLE FITNESS

(i) To display the names of all the products with price more than 20000.
(ii) To display the names of all products by the manufacturer „Aone‟.
(iii) To change the price data of all the products by applying 25% discount reduction.
(iv) To add a new row for product with the details: „P7‟, „Vibro Exerciser‟, 28000, „Aone‟.

(v) SELECT * FROM FITNESS WHERE MANUFACTURER LIKE ’%e’;

Question 10

Consider the table SUPPLIER given below. Write commands in MySQL for (i) to (iv) and output for
(v) to (vii). (i)
TABLE SUPPLIER
(iii) To count distinct City in the table.
(iv) To insert a new row in the table SUPPLIER.
110, “Bournvita”,‟ ABC‟, 170, „Delhi‟, 40.00 (Delhi 2012)

(v) SELECT Pname FROM SUPPLIER WHERE Supname IN ('Bread', 'Maggt');


(vi) SELECT COUNTCDISTINCT City) FROM SUPPLIER;
(vii) SELECT MAX(Price) FROM SUPPLIER WHERE City = ‘Kol kata’;

You might also like