SQL Sub-Queries Questions and Answers

  • By Mahesh Kankrale
  • March 28, 2024
  • SQL
SQL Sub-Queries Questions and Answers

SQL Sub-Queries Questions and Answers

Elevate your SQL skills with our collection of expertly crafted SQL sub-queries Questions and Answers. Master the art of SQL sub-queries and enhance your database querying abilities today.

Find the names of students who are enrolled in the ‘Mathematics’ course.

SELECT name

FROM student

WHERE course_id = (SELECT course_id FROM courses WHERE course_name = ‘Mathematics‘);

 

Retrieve the names and percentages of students who are enrolled in the ‘Mathematics’ course and are older than 20.

SELECT name, percentage

FROM student

WHERE course_id IN (SELECT course_id FROM courses WHERE course_name = ‘Mathematics‘)

AND age > 20;

 

Retrieve the city names where students with a percentage greater than 90%.

SELECT city_name, city_id

FROM city

WHERE city_id IN (SELECT city_id FROM student WHERE percentage > 90);

 

Find the average age of students in the ‘Computer Science’ course.

SELECT AVG(age)

FROM student

WHERE course_id = (SELECT course_id FROM courses WHERE course_name = ‘Computer Science‘);

 

List the names of students who live in cities with names starting with the letter ‘N’.

SELECT name,city_id, gender

FROM student

WHERE city_id IN (SELECT city_id FROM city WHERE city_name LIKEN%‘);

 

For Free, Demo classes Call: 020 7117 1500

Registration Link: Click Here!

 

Find the highest percentage among students who are enrolled in the ‘Physics’ course.

SELECT MAX(percentage)

FROM student

WHERE course_id = (SELECT course_id FROM courses WHERE course_name = ‘Physics‘);

 

List the names and percentages of students who live in the same city.

SELECT name, percentage

FROM student

WHERE city_id IN (SELECT city_id FROM student GROUP BY city_id HAVING COUNT(*) > 1);

 

Provide the names and ages of students who are older than the average age of all students.

SELECT name, age

FROM student

WHERE age > (SELECT AVG(age) FROM student);

 

List the names of cities that do not have any students residing in them.

SELECT city_name

FROM city

WHERE city_id NOT IN (SELECT city_id FROM student);

 

Provide the names of courses that have at least one student enrolled in them.

SELECT course_name

FROM courses

WHERE course_id IN (SELECT DISTINCT course_id FROM student);

 

Select the names of students who study Physics or Computer Science:

SELECT name FROM student WHERE course_id IN (SELECT course_id FROM courses WHERE course_name IN (‘Physics‘, ‘Computer Science‘));

 

Update the city of Mike Jones to Los Angeles:

UPDATE student SET city_id = (SELECT city_id FROM city WHERE city_name = ‘Los Angeles‘) 

WHERE name = ‘Mike Jones‘;

 

For Free, Demo classes Call: 020 7117 1500

Registration Link: SQL Training in Pune!

 

Increase the percentage of students who study Mathematics by 2.5:

UPDATE student 

SET percentage = percentage + 2.5 

WHERE course_id = (SELECT course_id FROM courses WHERE course_name = ‘Mathematics‘);

 

Delete male students who study Physics:

DELETE FROM student 

WHERE gender = ‘Male‘ 

AND course_id = (SELECT course_id FROM courses WHERE course_name = ‘Physics‘);

 

Delete students older than 25 who live in Chicago:

DELETE FROM student 

WHERE age > 25 

AND city_id = (SELECT city_id FROM city WHERE city_name = ‘Chicago‘);

 

Do watch our Channel to learn more: Click Here

Author:

Mahesh Kankrale

Call the Trainer and Book your free demo Class For SQL Call now!!!
| SevenMentor Pvt Ltd.

© Copyright 2021 | SevenMentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*