0% found this document useful (0 votes)
3 views10 pages

SQL Test Without Answers

Sql test

Uploaded by

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

SQL Test Without Answers

Sql test

Uploaded by

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

MYSQL Technical Evaluation – Time 1 Hour

Question 1 to 25: 1 Marks each; Qn 26 to 40, 2 marks each.


Your Name_______________________ Mob #:_______________________
Database Schema:
Let's assume the following schema:
• Students
o student_id (INT, PRIMARY KEY)
o student_name (VARCHAR(100))
o major (VARCHAR(50))
• Courses
o course_id (INT, PRIMARY KEY)
o course_name (VARCHAR(100))
o credits (INT)
• Enrollments
o enrollment_id (INT, PRIMARY KEY)
o student_id (INT, FOREIGN KEY referencing Students)
o course_id (INT, FOREIGN KEY referencing Courses)
o enrollment_date (DATE)
• Attendance
o attendance_id (INT, PRIMARY KEY)
o enrollment_id (INT, FOREIGN KEY referencing Enrollments)
o session_date (DATE)
o status (ENUM('Present', 'Absent', 'Late'))
• Marks
o mark_id (INT, PRIMARY KEY)
o enrollment_id (INT, FOREIGN KEY referencing Enrollments)
o exam_name (VARCHAR(50))
o marks_obtained (DECIMAL(5, 2))
o total_marks (DECIMAL(5, 2))
1. Which SQL keyword is used to retrieve data from one or more tables?
a) SELECT
b) FROM
c) WHERE
d) ORDER BY
2. Which SQL keyword is used to filter records based on a specified condition?
a) SELECT
b) FROM
c) WHERE
d) HAVING
3. Which SQL keyword is used to sort the result-set of a query?
a) SORT BY
b) ORDER BY
c) GROUP BY
d) ALIGN BY
4. Which SQL keyword is used to group rows that have the same values in one or more columns
into a summary row?
a) SORT BY
b) ORDER BY
c) GROUP BY
d) HAVING
5. Which SQL keyword is used to specify conditions on groups created by the GROUP BY clause?
a) WHERE
b) HAVING
c) AND
d) OR
6. What type of join returns only the rows that have matching values in both tables?
a) LEFT JOIN
b) RIGHT JOIN
c) INNER JOIN
d) FULL OUTER JOIN
7. What type of join returns all rows from the left table and the matching rows from the right table.
If there is no match in the right table,1 NULL is used?
a) LEFT JOIN
b) RIGHT JOIN
c) INNER JOIN
d) FULL OUTER JOIN
8. What type of join returns all rows from the right table and the matching rows from the left table.
If there is no match in the left table, NULL2 is used?
a) LEFT JOIN
b) RIGHT JOIN
c) INNER JOIN
d) FULL OUTER JOIN
9. Which SQL command is used to add new rows to a table?
a) ADD
b) INSERT INTO
c) CREATE ROW
d) UPDATE
10. Which SQL command is used to modify existing data in a table?
a) MODIFY
b) ALTER
c) UPDATE
d) CHANGE
11. Which SQL command is used to remove rows from a table based on a condition?
a) DELETE FROM
b) REMOVE
c) DROP ROW
d) CLEAR TABLE
12. Which SQL command is used to remove an entire table from the database?
a) DELETE TABLE
b) REMOVE TABLE
c) DROP TABLE
d) ERASE TABLE
13. What is a primary key constraint?
a) It ensures that all values in a column are unique.
b) It uniquely identifies each row in a table and cannot contain NULL values.
c) It establishes a link between two tables.
d) It restricts the values that can be inserted into a column.
14. What is a foreign key constraint?
a) It ensures that all values in a column are unique.
b) It uniquely identifies each row in a table.
c) It establishes and enforces a link between the data in two tables.
d) It restricts the data type of a column.
15. Which SQL aggregate function calculates the average of values in a column?
a) SUM()
b) COUNT()
c) AVG()
d) MAX()
16. Which SQL aggregate function counts the number of rows in a result set?
a) SUM()
b) COUNT()
c) AVG()
d) MIN()
17. Which SQL aggregate function finds the maximum value in a column?
a) MAX()
b) MIN()
c) SUM()
d) AVG()
18. Which SQL aggregate function calculates the sum of values in a column?
a) COUNT()
b) SUM()
c) AVG()
d) MAX()
19. What does the DISTINCT keyword do in a SELECT statement?
a) It sorts the result set.
b) It filters the result set based on a condition.
c) It returns only unique values.
d) It groups the result set.
20. Which SQL operator is used for pattern matching?
a) =
b) >
c) LIKE
d) IN
21. Which SQL operator is used to specify a range of values in a WHERE clause?
a) LIKE
b) IN
c) BETWEEN
d) EXISTS
22. Which SQL operator is used to check if a value exists in a list of values?
a) LIKE
b) IN
c) BETWEEN
d) EXISTS
23. What is a subquery in SQL?
a) A query that is executed independently of the main query.
b) A query nested inside another SQL query.
c) A query that modifies data in multiple tables.
d) A query that defines the structure of a table.
24. What is a transaction in the context of a database?
a) A single SQL statement.
b) A sequence of operations treated as a single logical unit of work.
c) A way to define table relationships.
d) A method for backing up database data.
25. Which SQL command is used to undo changes made within the current transaction?
a) SAVE
b) COMMIT
c) ROLLBACK
d) UNDO

26. Write a SQL query to retrieve the names of all students who are enrolled in the course named
'Database Management'.
a) SELECT s.student_name FROM Students s JOIN Enrollments e ON s.student_id = e.student_id JOIN
Courses c ON e.course_id = c.course_id WHERE c.course_name = 'Database Management';
b) SELECT student_name FROM Students WHERE student_id IN (SELECT student_id FROM
Enrollments WHERE course_id IN (SELECT course_id FROM Courses WHERE course_name =
'Database Management'));
c) SELECT student_name FROM Students, Enrollments, Courses WHERE Students.student_id =
Enrollments.student_id AND Enrollments.course_id = Courses.course_id AND Courses.course_name
= 'Database Management';
d) All of the above.
27. Write a SQL query to find the total number of students enrolled in each course, displaying the
course name and the count, ordered by the count in descending order.
a) SELECT c.course_name, COUNT(e.student_id) AS total_students FROM Courses c JOIN Enrollments
e ON c.course_id = e.course_id GROUP BY c.course_name ORDER BY total_students DESC;
b) SELECT course_name, COUNT(*) FROM Enrollments GROUP BY course_id ORDER BY COUNT(*)
DESC;
c) SELECT course_name, COUNT(student_id) FROM Enrollments GROUP BY course_id ORDER BY
COUNT(student_id) DESC;
d) SELECT c.course_name, COUNT(*) FROM Courses c, Enrollments e WHERE c.course_id =
e.course_id GROUP BY c.course_name ORDER BY COUNT(*) DESC;
28. Write a SQL query to list the students who have been marked 'Absent' in at least 3 different
sessions.
a) SELECT s.student_name FROM Students s JOIN Enrollments e ON s.student_id = e.student_id JOIN
Attendance a ON e.enrollment_id = a.enrollment_id WHERE a.status = 'Absent' GROUP BY
s.student_id HAVING COUNT(DISTINCT a.session_date) >= 3;
b) SELECT student_name FROM Students WHERE student_id IN (SELECT student_id FROM
Enrollments WHERE enrollment_id IN (SELECT enrollment_id FROM Attendance WHERE status =
'Absent' GROUP BY enrollment_id HAVING COUNT(*) >= 3));
c) SELECT s.student_name FROM Students s JOIN Enrollments e ON s.student_id = e.student_id JOIN
Attendance a ON e.enrollment_id = a.enrollment_id WHERE a.status = 'Absent' GROUP BY
s.student_name HAVING COUNT(*) >= 3;
d) SELECT s.student_name FROM Students s, Enrollments e, Attendance a WHERE s.student_id =
e.student_id AND e.enrollment_id = a.enrollment_id AND a.status = 'Absent' GROUP BY
s.student_name HAVING COUNT(a.session_date) >= 3;
29. Write a SQL query to find the average marks obtained by each student across all exams they
have taken. Display student name and average marks.
a) SELECT s.student_name, AVG(m.marks_obtained) AS average_marks FROM Students s JOIN
Enrollments e ON s.student_id = e.student_id JOIN Marks m ON e.enrollment_id = m.enrollment_id
GROUP BY s.student_name;
b) SELECT student_name, AVG(marks_obtained) FROM Students JOIN Enrollments USING
(student_id) JOIN Marks USING (enrollment_id) GROUP BY student_name;
c) SELECT s.student_name, AVG(marks_obtained) FROM Students s, Enrollments e, Marks m WHERE
s.student_id = e.student_id AND e.enrollment_id = m.enrollment_id GROUP BY s.student_name;
d) All of the above.
30. Write a SQL query to retrieve the names of courses where the average attendance (percentage
of 'Present' sessions out of total sessions) is less than 75%.
a) SELECT c.course_name FROM Courses c JOIN Enrollments e ON c.course_id = e.course_id JOIN
Attendance a ON e.enrollment_id = a.enrollment_id GROUP BY c.course_name HAVING (SUM(CASE
WHEN a.status = 'Present' THEN 1 ELSE 0 END) / COUNT(*)) < 0.75;
b) SELECT course_name FROM Courses WHERE course_id IN (SELECT course_id FROM Enrollments
WHERE enrollment_id IN (SELECT enrollment_id FROM Attendance GROUP BY enrollment_id
HAVING (SUM(CASE WHEN status = 'Present' THEN 1 ELSE 0 END) / COUNT(*)) < 0.75));
c) SELECT c.course_name FROM Courses c JOIN Enrollments e ON c.course_id = e.course_id LEFT
JOIN Attendance a ON e.enrollment_id = a.enrollment_id GROUP BY c.course_name HAVING
AVG(CASE WHEN a.status = 'Present' THEN 1 ELSE 0 END) < 0.75;
d) None of the above.
31. Write a SQL query to find the student who has the highest total marks across all exams. Display
student name and total marks. (Assume marks_obtained is the score and total_marks is the
maximum possible score for each exam).
a) SELECT s.student_name, SUM(m.marks_obtained) AS total_obtained FROM Students s JOIN
Enrollments e ON s.student_id = e.student_id JOIN Marks m ON e.enrollment_id = m.enrollment_id
GROUP BY s.student_name ORDER BY total_obtained DESC LIMIT 1;
b) SELECT student_name, SUM(marks_obtained) FROM Students JOIN Enrollments USING
(student_id) JOIN Marks USING (enrollment_id) GROUP BY student_name ORDER BY
SUM(marks_obtained) DESC LIMIT 1;
c) SELECT s.student_name, SUM(m.marks_obtained) FROM Students s, Enrollments e, Marks m
WHERE s.student_id = e.student_id AND e.enrollment_id = m.enrollment_id GROUP BY
s.student_name ORDER BY SUM(m.marks_obtained) DESC LIMIT 1;
d) All of the above.
32. Write a SQL query to list the courses that have no enrollments.
a) SELECT c.course_name FROM Courses c WHERE NOT EXISTS (SELECT 1 FROM Enrollments e
WHERE c.course_id = e.course_id);
b) SELECT course_name FROM Courses WHERE course_id NOT IN (SELECT DISTINCT course_id FROM
Enrollments);
c) SELECT c.course_name FROM Courses c LEFT JOIN Enrollments e ON c.course_id = e.course_id
WHERE e.enrollment_id IS NULL;
d) All of the above.
33. Write a SQL query to find the student(s) who have the highest number of 'Late' attendances.
Display student name and the count of 'Late' attendances.
a) SELECT s.student_name, COUNT(a.attendance_id) AS late_count FROM Students s JOIN
Enrollments e ON s.student_id = e.student_id JOIN Attendance a ON e.enrollment_id =
a.enrollment_id WHERE a.status = 'Late' GROUP BY s.student_name ORDER BY late_count DESC
LIMIT 1;
b) SELECT student_name, COUNT(*) FROM Students JOIN Enrollments USING (student_id) JOIN
Attendance USING (enrollment_id) WHERE status = 'Late' GROUP BY student_name ORDER BY
COUNT(*) DESC LIMIT 1;
c) SELECT s.student_name, COUNT(*) FROM Students s, Enrollments e, Attendance a WHERE
s.student_id = e.student_id AND e.enrollment_id = a.enrollment_id AND a.status = 'Late' GROUP BY
s.student_name ORDER BY COUNT(*) DESC LIMIT 1;
d) SELECT s.student_name, COUNT(a.attendance_id) FROM Students s JOIN Enrollments e ON
s.student_id = e.student_id JOIN Attendance a ON e.enrollment_id = a.enrollment_id WHERE
a.status = 'Late' GROUP BY s.student_name HAVING COUNT(*) = (SELECT MAX(late_count) FROM
(SELECT COUNT(*) AS late_count FROM Attendance WHERE status = 'Late' GROUP BY enrollment_id)
AS subquery);

34. Write a SQL query to retrieve the names of students who have scored more than 80% in any of
the exams they have taken.
a) SELECT DISTINCT s.student_name FROM Students s JOIN Enrollments e ON s.student_id =
e.student_id JOIN Marks m ON e.enrollment_id = m.enrollment_id WHERE (m.marks_obtained /
m.total_marks) > 0.8;
b) SELECT DISTINCT student_name FROM Students WHERE student_id IN (SELECT student_id FROM
Enrollments WHERE enrollment_id IN (SELECT enrollment_id FROM Marks WHERE (marks_obtained
/ total_marks) > 0.8));
c) SELECT DISTINCT s.student_name FROM Students s, Enrollments e, Marks m WHERE s.student_id
= e.student_id AND e.enrollment_id = m.enrollment_id AND (m.marks_obtained / m.total_marks) >
0.8;
d) All of the above.
35. Write a SQL query to find the course(s) with the highest average marks across all enrollments in
that course. Display the course name and the average mark.
a) SELECT c.course_name, AVG(m.marks_obtained) AS avg_marks FROM Courses c JOIN Enrollments
e ON c.course_id = e.course_id JOIN Marks m ON e.enrollment_id = m.enrollment_id GROUP BY
c.course_name ORDER BY avg_marks DESC LIMIT 1;
b) SELECT course_name, AVG(marks_obtained) FROM Enrollments JOIN Courses USING (course_id)
JOIN Marks USING (enrollment_id) GROUP BY course_name ORDER BY AVG(marks_obtained) DESC
LIMIT 1;
c) SELECT c.course_name, AVG(m.marks_obtained) FROM Courses c, Enrollments e, Marks m WHERE
c.course_id = e.course_id AND e.enrollment_id = m.enrollment_id GROUP BY c.course_name ORDER
BY AVG(m.marks_obtained) DESC LIMIT 1;
d) SELECT c.course_name, AVG(m.marks_obtained) FROM Courses c JOIN Enrollments e ON
c.course_id = e.course_id JOIN Marks m ON e.enrollment_id = m.enrollment_id GROUP BY
c.course_name HAVING AVG(m.marks_obtained) = (SELECT MAX(avg_marks) FROM (SELECT
AVG(marks_obtained) AS avg_marks, course_id FROM Marks GROUP BY course_id) AS subquery)
LIMIT 1;
36. Write a SQL query to list students who have attended all sessions for at least one course they are
enrolled in.
a) SELECT s.student_name FROM Students s JOIN Enrollments e ON s.student_id = e.student_id JOIN
Attendance a ON e.enrollment_id = a.enrollment_id WHERE a.status = 'Present' GROUP BY
e.enrollment_id HAVING COUNT(*) = (SELECT COUNT(*) FROM Attendance WHERE enrollment_id =
e.enrollment_id) INTERSECT SELECT s.student_name FROM Students s JOIN Enrollments e ON
s.student_id = e.student_id;
b) SELECT DISTINCT s.student_name FROM Students s JOIN Enrollments e ON s.student_id =
e.student_id WHERE NOT EXISTS (SELECT 1 FROM Attendance a WHERE a.enrollment_id =
e.enrollment_id AND a.status <> 'Present');
c) SELECT s.student_name FROM Students s JOIN Enrollments e ON s.student_id = e.student_id
WHERE e.enrollment_id IN (SELECT enrollment_id FROM Attendance GROUP BY enrollment_id
HAVING COUNT(*) = SUM(CASE WHEN status = 'Present' THEN 1 ELSE 0 END));
d) SELECT s.student_name FROM Students s, Enrollments e WHERE s.student_id = e.student_id AND
NOT EXISTS (SELECT 1 FROM Attendance a WHERE a.enrollment_id = e.enrollment_id AND a.status =
'Absent') AND NOT EXISTS (SELECT 1 FROM Attendance a WHERE a.enrollment_id = e.enrollment_id
AND a.status = 'Late');
37. Write a SQL query to find the students who are enrolled in more than two courses. Display their
names.
a) SELECT s.student_name FROM Students s JOIN Enrollments e ON s.student_id = e.student_id
GROUP BY s.student_name HAVING COUNT(DISTINCT e.course_id) > 2;
b) SELECT student_name FROM Students WHERE student_id IN (SELECT student_id FROM
Enrollments GROUP BY student_id HAVING COUNT(*) > 2);
c) SELECT s.student_name FROM Students s, Enrollments e WHERE s.student_id = e.student_id
GROUP BY s.student_name HAVING COUNT(e.course_id) > 2;
d) All of the above.
38. Write a SQL query to identify courses where the number of students enrolled is less than the
average number of students per course.
a) SELECT c.course_name FROM Courses c WHERE (SELECT COUNT(*) FROM Enrollments e WHERE
e.course_id = c.course_id) < (SELECT AVG(student_count) FROM (SELECT COUNT(student_id) AS
student_count FROM Enrollments GROUP BY course_id) AS course_counts);
b) SELECT course_name FROM Courses WHERE course_id IN (SELECT course_id FROM Enrollments
GROUP BY course_id HAVING COUNT(*) < (SELECT AVG(count) FROM (SELECT COUNT(*) AS count
FROM Enrollments GROUP BY course_id) AS avg_count));
c) SELECT c.course_name FROM Courses c WHERE EXISTS (SELECT 1 FROM Enrollments e WHERE
e.course_id = c.course_id GROUP BY e.course_id HAVING COUNT(*) < (SELECT AVG(ec) FROM
(SELECT COUNT(*) AS ec FROM Enrollments GROUP BY course_id) AS avg_enrollment));
d) All of the above.
39. Write a SQL query to find the students who have never been absent in any of the sessions they
attended.
a) SELECT s.student_name FROM Students s JOIN Enrollments e ON s.student_id = e.student_id LEFT
JOIN Attendance a ON e.enrollment_id = a.enrollment_id GROUP BY s.student_name HAVING
SUM(CASE WHEN a.status = 'Absent' THEN 1 ELSE 0 END) = 0 AND COUNT(a.attendance_id) > 0;
b) SELECT student_name FROM Students WHERE student_id NOT IN (SELECT student_id FROM
Enrollments WHERE enrollment_id IN (SELECT enrollment_id FROM Attendance WHERE status =
'Absent'));
c) SELECT s.student_name FROM Students s WHERE NOT EXISTS (SELECT 1 FROM Enrollments e JOIN
Attendance a ON e.enrollment_id = a.enrollment_id WHERE s.student_id = e.student_id AND
a.status = 'Absent');
d) SELECT DISTINCT s.student_name FROM Students s JOIN Enrollments e ON s.student_id =
e.student_id JOIN Attendance a ON e.enrollment_id = a.enrollment_id WHERE a.status <> 'Absent'
GROUP BY s.student_name HAVING COUNT(a.attendance_id) > 0 AND COUNT(a.attendance_id) =
SUM(CASE WHEN a.status <> 'Absent' THEN 1 ELSE 0 END);
40. Write a SQL query to list the students and the courses they are enrolled in, along with their
marks for each exam in that course.
a) SELECT s.student_name, c.course_name, m.exam_name, m.marks_obtained FROM Students s
JOIN Enrollments e ON s.student_id = e.student_id JOIN Courses c ON e.course_id = c.course_id JOIN
Marks m ON e.enrollment_id = m.enrollment_id;
b) SELECT student_name, course_name, exam_name, marks_obtained FROM Students JOIN
Enrollments USING (student_id) JOIN Courses USING (course_id) JOIN Marks USING (enrollment_id);
c) SELECT s.student_name, c.course_name, m.exam_name, m.marks_obtained FROM Students s,
Enrollments e, Courses c, Marks m WHERE s.student_id = e.student_id AND e.course_id =
c.course_id AND e.enrollment_id = m.enrollment_id;
d) All of the above.

You might also like