SQL Queries Practice Question
DBMS , IOE, BCA ,CSIT
SQL COMMANDS PRACTICE QUESTIONS
Structured Query Language is a standard Database language that is used to create, maintain, and retrieve the relational database.
Let us consider a STUDENT Table
STUDENT
Roll |
Name |
Address |
Phone |
Age |
1 |
Ram |
KTM |
9855123451 |
18 |
2 |
Ramesh |
PKR |
9852431543 |
18 |
3 |
Sujit |
BIR |
9856253131 |
20 |
4 |
Suresh |
KTM |
9856768971 |
18 |
Write a SQL command to :
- Retrieve attributes ROLL_NO and NAME of all students
SELECT roll, name FROM STUDENT;
- Retrieve ROLL_NO and NAME of the students whose ROLL_NO is greater than 1
SELECT roll, name FROM STUDENT where roll> 1;
- Retrieve all attributes of students whose ROLL_NO is greater than 1
SELECT * FROM STUDENT where roll> 1;
- Represent the relation in ascending order by AGE
SELECT * FROM STUDENT ORDER BY age;
- Retrieve distinct values of an address
SELECT DISTINCT address FROM STUDENT.
AGGREGATE FUNCTIONS
Aggregation functions are used to perform mathematical operations on data values of a relation.
- Count the number of Address in the above table
SELECT COUNT (Address) FROM STUDENT.
- Find the sum of all ages from the above table .
SELECT SUM(age) from STUDENT.
Similarly use MAX, MIN and AVG functions.
- Find the sum of ages of students who belong to the same City.
SELECT Address, SUM(age) FROM STUDENT GROUP BY (address).
HAVING AND WHERE CLAUSE
(where clause cannot be used with aggregates, but the having clause can.
The where clause works on row’s data, not on aggregated data. )
- Find the Roll and Name of student whose age >18
SELECT roll, name FROM STUDENT WHERE age>18.
- Find Address and sum of ages of Students from the same City and finally find those whose sum of ages > 30.
SELECT Address, SUM(age) AS total FROM STUDENT GROUP BY address HAVING total >18.
Address |
Total |
KTM |
36 |
JOINS
Student
EnrollNo |
Name |
Address |
1001 |
Ram |
KTM |
1002 |
Hari |
PKR |
1003 |
Shyam |
BIR |
1004 |
Sita |
DHR |
Course Table
CID |
EnrollNo |
1 |
1001 |
2 |
1001 |
3 |
1001 |
1 |
1002 |
2 |
1003 |
INNER JOIN
SELECT Course.CID,Student.Name FROM Student INNER JOIN Course ON Course.EnrollNo = Student.EnrollNo ORDER BY Course.CourseID;
CID |
Name |
1 |
Ram |
1 |
Hari |
2 |
Ram |
2 |
Shyam |
3 |
Ram |
Inner Join is basically performed by just selecting the records having the common values or the matching values in both tables. In case of no common values, no data is shown in the output.
OUTER JOIN
LEFT OUTER JOIN
SELECT Student.Name,Course.CID FROM Student LEFT OUTER JOIN Course ON Course.EnrollNo = Student.EnrollNo ORDER BY Course.CourseID;
Output
Name |
CID |
Sita |
Null |
Hari |
1 |
Ram |
1 |
Ram |
2 |
Shyam |
2 |
Ram |
3 |
RIGHT OUTER JOIN
Record
Roll |
Name |
Age |
1 |
Hari |
18 |
2 |
Ankur |
19 |
3 |
Rupesh |
18 |
4 |
Vayu |
15 |
5 |
Nabin |
13 |
6 |
Suraj |
15 |
7 |
Ankit |
19 |
8 |
Bhuwan |
18 |
Course
CID |
Roll |
1 |
1 |
2 |
2 |
2 |
3 |
3 |
4 |
1 |
5 |
4 |
9 |
5 |
10 |
4 |
11 |
SELECT Record.NAME,Course.CID FROM Record RIGHT JOIN Course ON Course.Roll = Record.Roll;
Output
Name |
CID |
Hari |
1 |
Ankur |
2 |
Rupesh |
2 |
Vayu |
3 |
Nabin |
1 |
Null |
4 |
Null |
5 |
Null |
4 |
Full Join
SELECT Record.NAME,Course.CID FROM Record FULL JOIN Course ON Course.Roll = Record.Roll ;
Name |
CID |
Hari |
1 |
Ankur |
2 |
Rupesh |
2 |
Vayu |
3 |
Nabin |
1 |
Suraj |
Null |
Ankit |
Null |
Bhuwan |
Null |
Null |
4 |
Null |
5 |
Null |
4 |
SUBQUERIES
DATABASE
Name |
Roll |
Address |
Phone |
Ram |
1 |
KTM |
9874 |
Raj |
2 |
BRJ |
9832 |
Sasita |
3 |
BIR |
9811 |
Rabi |
4 |
POK |
9877 |
Suman |
5 |
DHR |
0945 |
STUDENT
Name |
Roll |
Section |
Rabi |
4 |
A |
Suman |
5 |
B |
Raj |
2 |
A |
- Display NAME, ADDRESS, PHONE of the students from DATABASE table whose section is A
SELECT NAME, LOCATION, PHONE FROM DATABASE WHERE ROLL IN (SELECT ROLL_NO FROM STUDENT WHERE SECTION='A');