Review of Database Concepts & SQL — Question 36
Back to all questionsWrite SQL commands for (a) to (d) and write the output for (e) and (f) on the basis of given table GRADUATE:
Table: GRADUATE
| S NO | NAME | STIPEND | SUBJECT | AVERAGE | RANK |
|---|---|---|---|---|---|
| 1 | KARAN | 400 | PHYSICS | 68 | 1 |
| 2 | RAJ | 450 | CHEMISTRY | 68 | 1 |
| 3 | DEEP | 300 | MATHS | 62 | 2 |
| 4 | DIVYA | 350 | CHEMISTRY | 63 | 1 |
| 5 | GAURAV | 500 | PHYSICS | 70 | 1 |
| 6 | MANAV | 400 | CHEMISTRY | 55 | 2 |
| 7 | VARUN | 250 | MATHS | 64 | 1 |
| 8 | LIZA | 450 | COMPUTER | 68 | 1 |
| 9 | PUJA | 500 | PHYSICS | 62 | 1 |
| 10 | NISHA | 300 | COMPUTER | 57 | 2 |
(a) List the names of those students who have obtained rank 1.
(b) Display a list of all those names whose average is greater than 65.
(c) Display the names of those students who have opted for computer as a subject with average of more than 60.
(d) List the names of all students whose name ends with 'a'.
(e) SELECT * FROM GRADUATE WHERE Subject = "Physics";
(f) SELECT DISTINCT RANK FROM GRADUATE;
(a)
SELECT NAME FROM GRADUATE
WHERE `RANK` = 1;+--------+
| NAME |
+--------+
| KARAN |
| RAJ |
| DIVYA |
| GAURAV |
| VARUN |
| LIZA |
| PUJA |
+--------+
(b)
SELECT NAME
FROM GRADUATE
WHERE AVERAGE > 65;+--------+
| NAME |
+--------+
| KARAN |
| RAJ |
| GAURAV |
| LIZA |
+--------+
(c)
SELECT NAME
FROM GRADUATE
WHERE SUBJECT = 'COMPUTER' AND AVERAGE > 60;+------+
| NAME |
+------+
| LIZA |
+------+
(d)
SELECT NAME FROM GRADUATE
WHERE NAME LIKE "%a";+-------+
| NAME |
+-------+
| DIVYA |
| LIZA |
| PUJA |
| NISHA |
+-------+
(e)
SELECT * FROM GRADUATE WHERE Subject = "Physics";+-------+--------+---------+---------+---------+------+
| S.No. | name | stipend | subject | average | RANK |
+-------+--------+---------+---------+---------+------+
| 1 | KARAN | 400 | PHYSICS | 68 | 1 |
| 5 | GAURAV | 500 | PHYSICS | 70 | 1 |
| 9 | PUJA | 500 | PHYSICS | 62 | 1 |
+-------+--------+---------+---------+---------+------+
(f) Since 'RANK' is a reserved keyword in SQL, we encounter an error while running this query. To avoid such errors, we can enclose the column name 'RANK' in backticks to treat it as a literal identifier.
The corrected query is :
SELECT DISTINCT `RANK` FROM GRADUATE;+------+
| RANK |
+------+
| 1 |
| 2 |
+------+