SQL Tutorial 2 with Answers – DIT2022
DQL example
SELECT expressions
FROM TABLES
WHERE conditions;
1. Select all records from a table - eg: select all students’ details /records
SELECT * FROM tblstudent;
2. Select records related to one field/column from a table - eg: select all students’ name / who are the students in
school
SELECT std_name FROM tblstudent;
3. Select records related to several fields from a table - eg: select all students’ name and their date of birth
SELECT std_name, dob FROM tblstudent;
4. Select top records from a table
SELECT TOP 3 * FROM tblstudent; SELECT TOP 3 * FROM tblstudent;
ORDER BY std_name DESC;
5. Selects only the DISTINCT values from the column - - eg: from which areas students coming from?
SELECT DISTINCT address2 FROM tblstudent ;
6. Select records according to a criterion – eg: select all student details those who are from Colombo
SELECT * FROM tblstudent WHERE address2=”Colombo”;
7. Select records according to a criterion – eg: Who are the students coming from Colombo ?
SELECT std_name FROM tblstudent WHERE address2=”Colombo”;
- eg: Who are the teachers in Pannala ?
SELECT tch_name FROM tblteacher WHERE address="pannala";
- eg: What are the courses available more than 12 months duration?
SELECT cs_name FROM tblcourse WHERE duration>12;
8. Select records according to several criterion – eg: Who are the students in 13A class coming from Gonawila ?
SELECT std_name FROM tblstudent WHERE address2="Gonawila" AND cls_id="13A";
- eg: Who are the students coming from Gonawila or Kurunegala?
SELECT std_name FROM tblstudent WHERE address2 IN ("Gonawila", "Kurunegala");
SELECT std_name FROM tblstudent WHERE address2="Gonawila" OR address2="Kurunegala";
1 © Chandana K Jayathilake / DIT2022 / ICT Center, Wayamba University of Sri Lanka 12/23/2023
9. Use function to calculate/ -
-eg: What is the fee/price of most expensive course in the school?
SELECT MAX(fee) FROM tblcourse;
-eg: How many teachers in the school?
SELECT COUNT(tch_id) FROM tblteacher;
-eg: How many teachers from colombo ?
SELECT COUNT(tch_id) FROM tblteacher WHERE address="Colombo";
-eg: Show each student’s birthyear.
SELECT std_name, YEAR(dob) as BirthYear FROM tblstudent;
-eg: Calculate each student’s age.
SELECT std_name, YEAR(Now)-YEAR(dob) as Age FROM tblstudent;
-eg: How many courses available more than 12 months duration?
SELECT COUNT(cs_id) FROM tblcourse WHERE duration>12;
10.Use function with grouping
-eg: Show the number of students in each class. / How many students in each class?
SELECT COUNT(std_id) AS No_of_Students, cls_id AS Class FROM tblstudent GROUP BY cls_id;
-eg: What are the class/s having more than 2 students?
SELECT COUNT(std_id), cls_id FROM tblstudent GROUP BY cls_id HAVING COUNT(std_id) > 2;
11.Use LIKE and Wildcards
-eg: selects all grade 12 student’s
SELECT * FROM tblstudent WHERE cls_id LIKE "12*";
-eg: selects all student except grade 12
SELECT * FROM tblstudent WHERE cls_id NOT LIKE "12*";
-eg: selects all student whose address1 start with letter m
SELECT * FROM tblstudent WHERE address1 LIKE "m*";
12.Use Join (relational tables)
- eg: Who are the class-teachers
SELECT tblteacher.tch_name FROM tblteacher, tblclass WHERE tblteacher.tch_id = tblclass.tch_id;
SELECT tch_name FROM tblteacher INNER JOIN tblclass ON tblteacher.tch_id = tblclass.tch_id;
- eg: Who are the class-teachers from Colombo?
SELECT tch_name FROM tblteacher, tblclass
WHERE tblteacher.tch_id = tblclass.tch_id AND tblteacher.address="Colombo";
-eg: How many class-teachers in the school from Colombo?
SELECT COUNT(tblteacher.tch_id) FROM tblteacher, tblclass
WHERE tblteacher.tch_id = tblclass.tch_id AND tblteacher.address="Colombo";
2 © Chandana K Jayathilake / DIT2022 / ICT Center, Wayamba University of Sri Lanka 12/23/2023
DML example
13. Insert data into table - – eg: Add a new teacher ‘T009’ Sagarika from Colombo
INSERT INTO tblteacher (tch_id, tch_name, address) VALUES ("T009","Sagarika","Pannala");
INSERT INTO tblpayment (pid, std_id, term) VALUES ("P01","S001","1");
14.Update table – eg: Change the address of the T009 teacher to Colombo
UPDATE tblteacher SET address='Colombo' WHERE tch_id='T009';
15.Delete a record from a table - – eg: Remove the ‘T009’ teacher
DELETE FROM tblteacher WHERE tch_name=”Sagarika”;
DDL example
16.Create a new table - – eg: create payment table
CREATE TABLE tblpayment ( pid varchar(3), std_id varchar(4), term varchar(2));
Create table with primary. foreign key constrains
CREATE TABLE tblpayment2 ( pid varchar(3) NOT NULL, std_id varchar(4), term varchar(2),
PRIMARY KEY (pid), FOREIGN KEY (std_id) REFERENCES tblstudent(std_id) );
17.Delete a table - – eg: Delete the payment table
DROP TABLE tblpayment;
3 © Chandana K Jayathilake / DIT2022 / ICT Center, Wayamba University of Sri Lanka 12/23/2023