SQL Tuto2 With Answers
SQL Tuto2 With Answers
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;
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;
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");
14.Update table – eg: Change the address of the T009 teacher to Colombo
UPDATE tblteacher SET address='Colombo' WHERE tch_id='T009';
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