SQL Queries
SQL Queries
WAQ to print the name of manager who have at least one person
reporting to him
WAQ to print the detail of all employees who earn greater than any
IT_PROG
https://www.geeksforgeeks.org/sql-query-interview-questions/
Output:
SHIVANSH
UMESH
RAKESH
RADHA
KUSH
PREM
PANKAJ
NAVLEEN
Output:
Computer Science
Mathematics
Biology
Chemistry
Physics
History
English
Output:
Shi
Ume
Rak
Rad
Kus
Pre
Pan
Nav
4. Write a SQL query to find the position of alphabet (‘a’) int the
first name column ‘Shivansh’ from Student table.
Output:
or
Output:
LENGTH(MAJ
MAJOR
OR)
Computer
16
Science
Mathematics 11
Biology 7
Chemistry 9
Physics 7
LENGTH(MAJ
MAJOR
OR)
History 7
English 7
Output:
ShivAnsh
Umesh
RAkesh
RAdhA
Kush
Prem
PAnkAj
NAvleen
Output:
Shivansh Mahajan
Umesh Sharma
Rakesh Kumar
Radha Sharma
Kush Kumar
Prem Chopra
Pankaj Vats
Navleen Kaur
8. Write a SQL query to print all Student details from Student table
order by FIRST_NAME Ascending and MAJOR Subject descending .
7.8 2021-09-01
205 Kush Kumar Physics
5 08:30:00
2021-09-01
208 Navleen Kaur 7 Mathematics
06:30:00
9.7 2021-09-01
207 Pankaj Vats English
8 02:30:00
9.5 2021-09-01
206 Prem Chopra History
6 09:24:00
2021-09-01
204 Radha Sharma 9.2 Chemistry
12:45:00
2021-09-01
203 Rakesh Kumar 5.6 Biology
10:00:00
8.4 2021-09-01
202 Umesh Sharma Mathematics
4 08:30:00
Output:
9.5 2021-09-01
206 Prem Chopra History
6 09:24:00
Output:
2021-09-01
203 Rakesh Kumar 5.6 Biology
10:00:00
2021-09-01
204 Radha Sharma 9.2 Chemistry
12:45:00
7.8 2021-09-01
205 Kush Kumar Physics
5 08:30:00
9.7 2021-09-01
207 Pankaj Vats English
8 02:30:00
2021-09-01 Mathemati
208 Navleen Kaur 7
06:30:00 cs
Output:
2021-09-01 Chemistr
204 Radha Sharma 9.2
12:45:00 y
13. Write an SQL query to print details of the Students whose GPA
lies between 9.00 and 9.99.
2021-09-01 Chemistr
204 Radha Sharma 9.2
12:45:00 y
9.5 2021-09-01
206 Prem Chopra History
6 09:24:00
9.7 2021-09-01
207 Pankaj Vats English
8 02:30:00
14. Write an SQL query to fetch the count of Students having Major
Subject ‘Computer Science’.
Output:
TOTAL_COU
MAJOR
NT
Computer
1
Science
15. Write an SQL query to fetch Students full names with GPA >=
8.5 and <= 9.5.
Output:
Shivansh Mahajan
Radha Sharma
16. Write an SQL query to fetch the no. of Students for each MAJOR
subject in the descending order.
Output:
COUNT(MAJ
MAJOR
OR)
Biology 1
Chemistry 1
Computer
1
Science
English 1
History 1
Physics 1
Mathematics 2
SELECT
Student.FIRST_NAME,
Student.LAST_NAME,
Scholarship.SCHOLARSHIP_AMOUNT,
Scholarship.SCHOLARSHIP_DATE
FROM
Student
INNER JOIN
Scholarship ON Student.STUDENT_ID = Scholarship.STUDENT_REF_ID;
Output:
2021-10-15
Shivansh Mahajan 5000
00:00:00
2022-08-18
Umesh Sharma 4500
00:00:00
2022-01-25
Rakesh Kumar 3000
00:00:00
00:00:00
18. Write an SQL query to show only odd rows from Student table.
Output:
2021-09-01
203 Rakesh Kumar 5.6 Biology
10:00:00
7.8 2021-09-01
205 Kush Kumar Physics
5 08:30:00
9.7 2021-09-01
207 Pankaj Vats English
8 02:30:00
19. Write an SQL query to show only even rows from Student table.
Output:
2021-09-01
204 Radha Sharma 9.2 Chemistry
12:45:00
9.5 2021-09-01
206 Prem Chopra History
6 09:24:00
2021-09-01 Mathemati
208 Navleen Kaur 7
06:30:00 cs
20. List all students and their scholarship amounts if they have
received any. If a student has not received a scholarship, display
NULL for the scholarship details.
SELECT
Student.FIRST_NAME,
Student.LAST_NAME,
Scholarship.SCHOLARSHIP_AMOUNT,
Scholarship.SCHOLARSHIP_DATE
FROM
Student
LEFT JOIN
Scholarship ON Student.STUDENT_ID = Scholarship.STUDENT_REF_ID;
21. Write an SQL query to show the top n (say 5) records of Student
table order by descending GPA.
Output:
9.7 2021-09-01
207 Pankaj Vats English
8 02:30:00
9.5 2021-09-01
206 Prem Chopra History
6 09:24:00
2021-09-01
204 Radha Sharma 9.2 Chemistry
12:45:00
8.4 2021-09-01
202 Umesh Sharma Mathematics
4 08:30:00
22. Write an SQL query to determine the nth (say n=5) highest GPA
from a table.
Output:
STUDENT_ FIRST_NA LAST_NA GP ENROLLMENT_D MAJO
ID ME ME A ATE R
23. Write an SQL query to determine the 5th highest GPA without
using LIMIT keyword.
Output:
24. Write an SQL query to fetch the list of Students with the same
GPA.
SELECT s1.* FROM Student s1, Student s2 WHERE s1.GPA = s2.GPA AND
s1.Student_id != s2.Student_id;
25. Write an SQL query to show the second highest GPA from a
Student table using sub-query.
Output:
9.56
26. Write an SQL query to show one row twice in results from a
table.
SELECT * FROM Student
UNION ALL
SELECT * FROM Student ORDER BY STUDENT_ID;
27. Write an SQL query to list STUDENT_ID who does not get
Scholarship.
Output:
204
205
206
207
208
28. Write an SQL query to fetch the first 50% records from a table.
29. Write an SQL query to fetch the MAJOR subject that have less
than 4 people in it.
Output:
MAJOR_COU
MAJOR
NT
Biology 1
Chemistry 1
Computer
1
Science
English 1
History 1
Mathematics 2
Physics 1
MAJOR_COU
MAJOR
NT
30. Write an SQL query to show all MAJOR subject along with the
number of people in there.
Output:
ALL_MAJ
MAJOR
OR
Biology 1
Chemistry 1
Computer
1
Science
English 1
History 1
Mathematics 2
Physics 1
31. Write an SQL query to show the last record from a table.
Output:
2021-09-01 Mathemati
208 Navleen Kaur 7
06:30:00 cs
33. Write an SQL query to fetch the last five records from a table.
SELECT *
FROM (
SELECT *
FROM Student
ORDER BY STUDENT_ID DESC
LIMIT 5
) AS subquery
ORDER BY STUDENT_ID;
Output:
2021-09-01
204 Radha Sharma 9.2 Chemistry
12:45:00
7.8 2021-09-01
205 Kush Kumar Physics
5 08:30:00
9.5 2021-09-01
206 Prem Chopra History
6 09:24:00
9.7 2021-09-01
207 Pankaj Vats English
8 02:30:00
2021-09-01 Mathemati
208 Navleen Kaur 7
06:30:00 cs
34. Write an SQL query to fetch three max GPA from a table using
co-related subquery.
9.78
9.56
9.2
35. Write an SQL query to fetch three min GPA from a table using co-
related subquery.
Output:
5.6
7
7.85
36. Write an SQL query to fetch nth max GPA from a table.
37. Write an SQL query to fetch MAJOR subjects along with the max
GPA in each of these MAJOR subjects.
Output:
MAXGP
MAJOR
A
Biology 5.6
Chemistry 9.2
Computer
8.79
Science
English 9.78
History 9.56
Mathematics 8.44
MAXGP
MAJOR
A
Physics 7.85
38. Write an SQL query to fetch the names of Students who has
highest GPA.
Output:
FIRST_NA GP
ME A
9.7
Pankaj
8
39. Write an SQL query to show the current date and time.
40. Write a query to create a new table which consists of data and
structure copied from the other table (say Student) or clone the
table named Student.
41. Write an SQL query to update the GPA of all the students in
‘Computer Science’ MAJOR subject to 7.5.
42. Write an SQL query to find the average GPA for each major.
Output:
AVERAGE_G
MAJOR
PA
Biology 5.6
AVERAGE_G
MAJOR
PA
Chemistry 9.2
Computer
4
Science
English 9.78
History 9.56
Mathematics 7.72
Physics 7.85
43. Write an SQL query to show the top 3 students with the highest
GPA.
Output:
9.7 2021-09-01
207 Pankaj Vats English
8 02:30:00
9.5 2021-09-01
206 Prem Chopra History
6 09:24:00
2021-09-01 Chemistr
204 Radha Sharma 9.2
12:45:00 y
44. Write an SQL query to find the number of students in each major
who have a GPA greater than 7.5.
Output:
HIGH_GPA_COU
MAJOR
NT
Biology 1
HIGH_GPA_COU
MAJOR
NT
Chemistry 1
Computer
1
Science
English 1
History 1
Mathematics 2
Physics 1
45. Write an SQL query to find the students who have the same GPA
as ‘Shivansh Mahajan’.
SELECT * FROM Student WHERE GPA = (SELECT GPA FROM Student WHERE
FIRST_NAME = 'Shivansh'
AND LAST_NAME = 'Mahajan');