SQL · Databases
43 questions · asked in 23 of 27 years (2000–2026)
MCQ 34 · MSQ 1 · NAT 81-mark 26 · 2-mark 17
2000Questions per year2026
Questions 21–25 of 43
Which of the following statements are TRUE about an SQL query?
P : An SQL query can contain a HAVING clause even if it does not have a GROUP BY clause
Q : An SQL query can contain a HAVING clause only if it has a GROUP BY clause
R : All attributes used in the GROUP BY clause must appear in the SELECT clause
S : Not all attributes used in the GROUP BY clause need to appear in the SELECT clause
P : An SQL query can contain a HAVING clause even if it does not have a GROUP BY clause
Q : An SQL query can contain a HAVING clause only if it has a GROUP BY clause
R : All attributes used in the GROUP BY clause must appear in the SELECT clause
S : Not all attributes used in the GROUP BY clause need to appear in the SELECT clause
Answer: (B)
P is true, Q is false. In standard SQL a HAVING clause can be used without GROUP BY. The whole table is then treated as one group. For example,
S is true, R is false. Columns in GROUP BY do not have to appear in SELECT. For example,
So P and S are true.
Answer: (B)
SELECT COUNT(*) FROM T HAVING COUNT(*) > 5 is valid.S is true, R is false. Columns in GROUP BY do not have to appear in SELECT. For example,
SELECT COUNT(*) FROM T GROUP BY dept is valid. (The reverse is the real rule: a plain column in SELECT must be in GROUP BY.)So P and S are true.
Answer: (B)
Database table by name Loan_Records is given below.
What is the output of the following SQL query?
What is the output of the following SQL query?
SELECT count(*)
FROM(
(SELECT Borrower, Bank_Manager FROM Loan_Records) AS S
NATURAL JOIN
(SELECT Bank_Manager, Loan_Amount FROM Loan_Records) AS T); Answer: (C)
The natural join uses the common column Bank_Manager:
- Sunderajan: S has 2 rows and T has 2 rows, so 2 2 = 4
- Ramgopal: 1 1 = 1
Total count = 5
Answer: 5
- Sunderajan: S has 2 rows and T has 2 rows, so 2 2 = 4
- Ramgopal: 1 1 = 1
Total count = 5
Answer: 5
Consider a database table T containing two columns X and Y each of type integer. After the creation of the table, one record (X=1,Y=1) is inserted in the table.
Let MX and MY denote the respective maximum values of X and Y among all records in the table at any point in time. Using MX and MY, new records are inserted in the table 128 times with X and Y values being MX+1, 2*MY+1 respectively. It may be noted that each time after the insertion, values of MX and MY change.
What will be the output of the following SQL query after the steps mentioned above are carried out?
SELECT Y FROM T WHERE X=7;
Let MX and MY denote the respective maximum values of X and Y among all records in the table at any point in time. Using MX and MY, new records are inserted in the table 128 times with X and Y values being MX+1, 2*MY+1 respectively. It may be noted that each time after the insertion, values of MX and MY change.
What will be the output of the following SQL query after the steps mentioned above are carried out?
SELECT Y FROM T WHERE X=7;
Answer: (A)
The table rows are (1, 1), (2, 3), (3, 7), (4, 15), and so on. Every new Y is 2Y + 1, so the pattern is .
For X = 7, the value of Y:
Answer: 127
For X = 7, the value of Y:
Answer: 127
A relational schema for a train reservation database is given below
What pids are returned by the following SQL query for the above instance of the tables?
What pids are returned by the following SQL query for the above instance of the tables?
SELECT pid
FROM Reservation
WHERE class = 'AC' AND
EXISTS (SELECT *
FROM Passenger
WHERE age > 65 AND
Passenger.pid = Reservation.pid) Answer: (C)
Reservations with class AC have pids 0, 1, 5 and 3.
Now check which of these have a passenger with age 65:
- pid 0: age is 65 no
- pid 1: age is 66 yes
- pid 5: no matching passenger no
- pid 3: age is 69 yes
Answer: 1, 3
Now check which of these have a passenger with age 65:
- pid 0: age is 65 no
- pid 1: age is 66 yes
- pid 5: no matching passenger no
- pid 3: age is 69 yes
Answer: 1, 3
Consider the following relational schema:
What does the following SQL query output?
Student(school-id,sch-roll-no,sname,saddress)
School(school-id,sch-name,sch-address,sch-phone)
Enrolment(school-id,sch-roll-no,erollno,examname)
ExamResult(erollno,examname,marks) What does the following SQL query output?
SELECT sch-name, COUNT (*)
FROM School C, Enrolment E, ExamResult R
WHERE E.school-id = C.school-id
AND
E.examname = R.examname AND E.erollno = R.erollno
AND
R.marks = 100 AND S.school-id IN (SELECT school-id
FROM student
GROUP BY school-id
HAVING COUNT (*) > 200)
GROUP By school-id Answer: (D)
The query has errors, so it does not run:
- The condition S.school-id IN (...) uses the alias S, but the FROM clause has only C, E and R. S is not defined.
- GROUP By school-id is ambiguous, because both School (C) and Enrolment (E) have a school-id column.
- sch-name is selected along with COUNT(*), but it is not in the GROUP BY. In standard SQL, every non-aggregate column in SELECT must appear in GROUP BY.
Any one of these makes the query fail, so options A, B and C, which describe an output, cannot be right.
Answer: (D)
- The condition S.school-id IN (...) uses the alias S, but the FROM clause has only C, E and R. S is not defined.
- GROUP By school-id is ambiguous, because both School (C) and Enrolment (E) have a school-id column.
- sch-name is selected along with COUNT(*), but it is not in the GROUP BY. In standard SQL, every non-aggregate column in SELECT must appear in GROUP BY.
Any one of these makes the query fail, so options A, B and C, which describe an output, cannot be right.
Answer: (D)
Keep practising