0% found this document useful (0 votes)
25 views17 pages

SQL Queries

practice Question for SQL Queries for beginner level

Uploaded by

Aryan Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
25 views17 pages

SQL Queries

practice Question for SQL Queries for beginner level

Uploaded by

Aryan Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 17

WAQ to print the name of all departments where no employee works

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/

1. Write a SQL query to fetch “FIRST_NAME” from the Student table


in upper case and use ALIAS name as STUDENT_NAME.

SELECT upper(FIRST_NAME) as STUDENT_NAME from Student;

Output:

SHIVANSH
UMESH
RAKESH
RADHA
KUSH
PREM
PANKAJ
NAVLEEN

2. Write a SQL query to fetch unique values of MAJOR Subjects from


Student table.

SELECT DISTINCT MAJOR from STUDENT;


or
SELECT MAJOR FROM STUDENT GROUP BY(MAJOR);

Output:

Computer Science
Mathematics
Biology
Chemistry
Physics
History
English

3. Write a SQL query to print the first 3 characters of FIRST_NAME


from Student table.
SELECT SUBSTRING(FIRST_NAME, 1, 3) FROM Student;

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.

SELECT INSTR(FIRST_NAME, 'a') FROM Student WHERE FIRST_NAME =


'Shivansh';

Output:

5. Write a SQL query that fetches the unique values of MAJOR


Subjects from Student table and print its length.

SELECT MAJOR,LENGTH(MAJOR) FROM Student GROUP BY(MAJOR);

or

SELECT DISTINCT MAJOR, LENGTH(MAJOR) FROM Student;

Output:

LENGTH(MAJ
MAJOR
OR)

Computer
16
Science

Mathematics 11

Biology 7

Chemistry 9

Physics 7
LENGTH(MAJ
MAJOR
OR)

History 7

English 7

6. Write a SQL query to print FIRST_NAME from the Student table


after replacing ‘a’ with ‘A’.

SELECT REPLACE(FIRST_NAME, 'a', 'A') FROM Student;

Output:

ShivAnsh
Umesh
RAkesh
RAdhA
Kush
Prem
PAnkAj
NAvleen

7. Write a SQL query to print the FIRST_NAME and LAST_NAME from


Student table into single column COMPLETE_NAME.

SELECT CONCAT(FIRST_NAME, ' ', LAST_NAME) AS COMPLETE_NAME FROM


Student;

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 .

SELECT * FROM Student ORDER BY FIRST_NAME , MAJOR DESC;


Output:

STUDENT_ FIRST_NA LAST_NA GP ENROLLMENT_D


MAJOR
ID ME ME A ATE

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.7 2021-09-01 Computer


201 Shivansh Mahajan
9 09:30:00 Science

8.4 2021-09-01
202 Umesh Sharma Mathematics
4 08:30:00

9. Write a SQL query to print details of the Students with the


FIRST_NAME as ‘Prem’ and ‘Shivansh’ from Student table.

SELECT * from Student WHERE FIRST_NAME IN ('Prem' , 'Shivansh');

Output:

STUDENT_ FIRST_NA LAST_NA GP ENROLLMENT_D


MAJOR
ID ME ME A ATE

8.7 2021-09-01 Computer


201 Shivansh Mahajan
9 09:30:00 Science

9.5 2021-09-01
206 Prem Chopra History
6 09:24:00

10. Write a SQL query to print details of the Students excluding


FIRST_NAME as ‘Prem’ and ‘Shivansh’ from Student table.
SELECT * from Student WHERE FIRST_NAME NOT IN ('Prem', 'Shivansh');

Output:

STUDENT_ FIRST_NA LAST_NA GP ENROLLMENT_D


MAJOR
ID ME ME A ATE

8.4 2021-09-01 Mathemati


202 Umesh Sharma
4 08:30:00 cs

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

11. Write a SQL query to print details of the Students whose


FIRST_NAME ends with ‘a’.

SELECT * FROM Student WHERE FIRST_NAME LIKE '%a';

Output:

STUDENT_ FIRST_NA LAST_NA GP ENROLLMENT_D


MAJOR
ID ME ME A ATE

2021-09-01 Chemistr
204 Radha Sharma 9.2
12:45:00 y

12. Write an SQL query to print details of the Students whose


FIRST_NAME ends with ‘a’ and contains six alphabets.

SELECT * FROM Student WHERE FIRST_NAME LIKE '_____a';

13. Write an SQL query to print details of the Students whose GPA
lies between 9.00 and 9.99.

SELECT * FROM Student WHERE GPA BETWEEN 9.00 AND 9.99;


Output:

STUDENT_ FIRST_NA LAST_NA GP ENROLLMENT_D


MAJOR
ID ME ME A ATE

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’.

SELECT Major, COUNT(*) as TOTAL_COUNT FROM Student WHERE MAJOR =


'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.

SELECT CONCAT(FIRST_NAME, ' ', LAST_NAME) AS FULL_NAME FROM Student


WHERE GPA BETWEEN 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.

SELECT MAJOR, COUNT(MAJOR) from Student group by MAJOR order by


COUNT(MAJOR) DESC;

Output:
COUNT(MAJ
MAJOR
OR)

Biology 1

Chemistry 1

Computer
1
Science

English 1

History 1

Physics 1

Mathematics 2

17. Display the details of students who have received scholarships,


including their names, scholarship amounts, and scholarship dates.

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:

FIRST_NA LAST_NA SCHOLARSHIP_AMO SCHOLARSHIP_D


ME ME UNT ATE

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

Shivansh Mahajan 4000 2021-10-15


FIRST_NA LAST_NA SCHOLARSHIP_AMO SCHOLARSHIP_D
ME ME UNT ATE

00:00:00

18. Write an SQL query to show only odd rows from Student table.

SELECT * FROM Student WHERE student_id % 2 != 0;

Output:

STUDENT_ FIRST_NA LAST_NA GP ENROLLMENT_D


MAJOR
ID ME ME A ATE

8.7 2021-09-01 Computer


201 Shivansh Mahajan
9 09:30:00 Science

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.

SELECT * FROM Student WHERE student_id % 2 = 0;

Output:

STUDENT_ FIRST_NA LAST_NA GP ENROLLMENT_D


MAJOR
ID ME ME A ATE

8.4 2021-09-01 Mathemati


202 Umesh Sharma
4 08:30:00 cs

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.

SELECT * from Student ORDER BY GPA DESC LIMIT 5;

Output:

STUDENT_ FIRST_NA LAST_NA GP ENROLLMENT_D


MAJOR
ID ME ME A ATE

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.7 2021-09-01 Computer


201 Shivansh Mahajan
9 09:30:00 Science

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.

SELECT * FROM Student ORDER BY GPA DESC LIMIT 5, 1;

Output:
STUDENT_ FIRST_NA LAST_NA GP ENROLLMENT_D MAJO
ID ME ME A ATE R

7.8 2021-09-01 Physic


205 Kush Kumar
5 08:30:00 s

23. Write an SQL query to determine the 5th highest GPA without
using LIMIT keyword.

SELECT * FROM Student s1


WHERE 4 = (
SELECT COUNT(DISTINCT (s2.GPA))
FROM Student s2
WHERE s2.GPA >= s1.GPA
);

Output:

STUDENT_ FIRST_NA LAST_NA GP ENROLLMENT_D


MAJOR
ID ME ME A ATE

8.7 2021-09-01 Computer


201 Shivansh Mahajan
9 09:30:00 Science

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.

SELECT MAX(GPA) FROM Student


WHERE GPA NOT IN(SELECT MAX(GPA) FROM Student);

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.

SELECT STUDENT_ID FROM Student


WHERE STUDENT_ID NOT IN (SELECT STUDENT_REF_ID FROM Scholarship);

Output:

204
205
206
207
208

28. Write an SQL query to fetch the first 50% records from a table.

SELECT * FROM Student WHERE STUDENT_ID <= (SELECT


COUNT(STUDENT_ID)/2 FROM Student);

29. Write an SQL query to fetch the MAJOR subject that have less
than 4 people in it.

SELECT MAJOR, COUNT(MAJOR) AS MAJOR_COUNT FROM Student GROUP BY


MAJOR HAVING COUNT(MAJOR) < 4;

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.

SELECT MAJOR, COUNT(MAJOR) AS ALL_MAJOR FROM Student GROUP BY


MAJOR;

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.

SELECT * FROM Student WHERE STUDENT_ID = (SELECT MAX(STUDENT_ID)


FROM STUDENT);

Output:

STUDENT_ FIRST_NA LAST_NA GP ENROLLMENT_D


MAJOR
ID ME ME A ATE

2021-09-01 Mathemati
208 Navleen Kaur 7
06:30:00 cs

32. Write an SQL query to fetch the first row of a table.

SELECT * FROM Student WHERE STUDENT_ID = (SELECT MIN(STUDENT_ID)


FROM Student);
Output:

STUDENT_ FIRST_NA LAST_NA GP ENROLLMENT_D


MAJOR
ID ME ME A ATE

8.7 2021-09-01 Computer


201 Shivansh Mahajan
9 09:30:00 Science

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:

STUDENT_ FIRST_NA LAST_NA GP ENROLLMENT_D


MAJOR
ID ME ME A ATE

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.

SELECT DISTINCT GPA FROM Student S1


WHERE 3 >= (SELECT COUNT(DISTINCT GPA) FROM Student S2 WHERE
S1.GPA <= S2.GPA) ORDER BY S1.GPA DESC;
Output:

9.78
9.56
9.2

35. Write an SQL query to fetch three min GPA from a table using co-
related subquery.

SELECT DISTINCT GPA FROM Student S1


WHERE 3 >= (SELECT COUNT(DISTINCT GPA) FROM Student S2 WHERE
S1.GPA >= S2.GPA) ORDER BY S1.GPA;

Output:

5.6
7
7.85

36. Write an SQL query to fetch nth max GPA from a table.

SELECT DISTINCT GPA FROM Student S1


WHERE n >= (SELECT COUNT(DISTINCT GPA) FROM Student S2 WHERE
S1.GPA <= S2.GPA) ORDER BY S1.GPA DESC;

37. Write an SQL query to fetch MAJOR subjects along with the max
GPA in each of these MAJOR subjects.

SELECT MAJOR, MAX(GPA) as MAXGPA FROM Student GROUP BY MAJOR;

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.

SELECT FIRST_NAME, GPA FROM Student WHERE GPA = (SELECT MAX(GPA)


FROM Student);

Output:

FIRST_NA GP
ME A

9.7
Pankaj
8

39. Write an SQL query to show the current date and time.

Query to get current date :


SELECT CURDATE();
Query to get current date and time :
SELECT NOW();

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.

CREATE TABLE CloneTable AS SELECT * FROM Student;

41. Write an SQL query to update the GPA of all the students in
‘Computer Science’ MAJOR subject to 7.5.

UPDATE Student SET GPA = 4.0 WHERE MAJOR = 'Computer Science';

42. Write an SQL query to find the average GPA for each major.

SELECT MAJOR, AVG(GPA) AS AVERAGE_GPA FROM Student GROUP BY 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.

SELECT * FROM Student ORDER BY GPA DESC LIMIT 3;

Output:

STUDENT_ FIRST_NA LAST_NA GP ENROLLMENT_D


MAJOR
ID ME ME A ATE

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.

SELECT MAJOR, COUNT(STUDENT_ID) AS HIGH_GPA_COUNT FROM Student


WHERE GPA > 3.5 GROUP BY MAJOR;

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');

You might also like