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

SQL Questions

To get the maximum salary for each department, you need to use GROUP BY clause along with MAX aggregate function. The correct query would be: SELECT Deptcode, MAX(Salary) FROM Employee GROUP BY Deptcode; This will return the maximum salary for each unique department code by grouping the rows based on Deptcode column and applying MAX function to calculate maximum salary within each group. Without GROUP BY, MAX was calculating the overall maximum salary across all departments instead of per department. So the addition of GROUP BY clause is necessary to get the desired output of maximum salary for each department separately.

Uploaded by

Leela 2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

SQL Questions

To get the maximum salary for each department, you need to use GROUP BY clause along with MAX aggregate function. The correct query would be: SELECT Deptcode, MAX(Salary) FROM Employee GROUP BY Deptcode; This will return the maximum salary for each unique department code by grouping the rows based on Deptcode column and applying MAX function to calculate maximum salary within each group. Without GROUP BY, MAX was calculating the overall maximum salary across all departments instead of per department. So the addition of GROUP BY clause is necessary to get the desired output of maximum salary for each department separately.

Uploaded by

Leela 2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Q:Sports Authority of India (SAI) has recruited some players with the details given below in

table ‘sports’. Now, SAI wants to perform some queries on sports table that is already been
created in database. Therefore, Write mentioned five SQL queries to facilitate SAI:-
Table: sports

id pname age sname pay gender


1 Kabir 35 Lawn Tennis 15000 null
2 Rekha 29 Karate 12000 F
3 Kanishk 34 Squash 20000 M
4 Sarita 29 Squash 21000 null
5 Ravi 24 Karate 25000 M

Query (1): Show details of those players whose name start with ‘K’.
Query(2):Delete the records of those players whose gender in unknown (null).
Query (3): Show name of those players whose age is between 20 and 30.
Query (4): Show details of those players who play either Karate or Squash.
Query (5):Increase (update) the 15% pay of all the players.
Ans:
(1) select * from sports where pname like ‘K%’;
(2) delete from sports where gender is null;
(3) select pname from sports where age between 20 and 30;
(4) select * from sports where sname in(‘Karate’,‘Squash’);
(5) update sports set pay=pay+(pay*15)/100;
Q: Perform the below queries in MySql database using SQL:

Ans:

(i) 63

(ii) 800

(iii) 475
Write SQL commands for the following queries (i) to (v) based on the relations Teacher and Posting given
below:
TABLE : TEACHER
T_ID Name Age Department Date_of_join Salary Gender
1 Jugal 34 Computer Sc 10-01-2017 12000 M
2 Sharmila 31 History 24-03-2008 20000 F
3 Sandeep 32 Mathematics 12-12-2016 30000 M
4 Sangeeta 35 History 01-07-2015 40000 F
5 Rakesh 42 Mathematics 05-09-2007 25000 M
6 Shyam 50 History 27-06-2008 30000 M
7 Shiv Om 44 Computer Sc 25-02-2017 21000 M
8 Shalakha 33 Mathematics 31-07-2018 20000 F

i. To show total salary paid to each department.


ii. To list the all Department whose total salary paid is over 50000.
iii. To list the names of all teachers with their date of joining in ascending order.
iv. To display number of teachers of each gender.
v. To display name, bonus for each teacher where bonus is 10% of salary in descending order of their name.

Consider the following table named “Product”, showing details of products being sold in a grocery shop.
Table : Product
PCode PName UPrice Manufacturer
P01 Washing Powder 120 Surf
P02 ToothPaste 54 Colgate
P03 Soap 25 Lux
P04 ToothPaste 65 Pepsodant
P05 Soap 38 Dove
P06 Shampoo 245 Dove

Write SQL queries for the (i) and (ii) and write output of queries (iii) ,(iv) and (v)
i. List the Product Code, Product name and price in descending order of their price

ii. Display the total number of products manufactured by each manufacturer.


iii. SELECT PName, Average(UPrice) FROM Product GROUP BY Pname;
iv. SELECT PName, MAX(UPrice), MIN(UPrice) FROM Product GROUP BY PName;
v. SELECT DISTINCT Manufacturer FROM Product order by Manufactures DESC;
Q. Madhu is using a table Employee. It has the following columns :

Code, Name, Salary, Deptcode.

She wants to display maximum salary department wise. She wrote the following command:

SELECT Deptcode, Max(Salary) FROM Employee; But she did not get the desired result. Rewrite
the above query with necessary changes to help her get the desired output.

You might also like