0% found this document useful (0 votes)
15 views

SQL1 Updated

SQL is a language used to manage data in relational database management systems. It allows users to retrieve, insert, modify and delete data as well as manage schema objects like tables and indexes. The document provides examples of SQL commands for data manipulation, data definition, data control and querying data.

Uploaded by

Hasitha Sanjaya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

SQL1 Updated

SQL is a language used to manage data in relational database management systems. It allows users to retrieve, insert, modify and delete data as well as manage schema objects like tables and indexes. The document provides examples of SQL commands for data manipulation, data definition, data control and querying data.

Uploaded by

Hasitha Sanjaya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Structured Query Language

M. Y Abdur Rahman
 SQL database computer language designed for the retrieval and
management of data in relational database management systems

 SQL support data manipulation[DML}


Structured  Retrieval of data contained in the database

Query  Insertion of data in to the database

Language
 Modification of data stored in the database
 Deletion of the data from the database
 Other than DML, SQL has DCL DDL
 DDL – Data Definition Language (create, drop, alter, truncate, comment,
SQL | DDL, rename)

DQL, DML  DQl – Data Query Language (select)

and DCL  DML – Data Manipulation Language (insert, update, delete, lock, call

Commands explain plan)

 DCL – Data Control Language (grant, revoke)


 Key: minimal set of attributes that uniquely identifies each tuple in
a relation.
 Candidate key: In a given relation there may be more than one set
of attributes that could be chosen as a key
 Primary key: when one the candidate key is selected as the
relation key it is called the primary.
 Alternative key: other candidate keys which are not selected as
keys the primary key.
 Composite key: a key consisting more than one attribute.

Composite key

Supplier_ID Item_id Item_name Quatity


 Foreign key: Is an attribute or set of attributes in one relation which is a
primary key of another relation.

Primary key

EMP_ID EMP_nam DEPT_NO DEPT_NO Dept


e Name

Foreign key
 Question 01: Create a database name School.
CREATE DATABASE school;
 Question 02: Create a table name student with the following fields
(student id - varchar (30) primary key, student fname - varchar (30),
student lname – varchar (30), student address varchar (30), city
varchar(30), student contact number int (10)).
SQL questions
CREATE TABLE student (student_id varchar (30) NOT NULL primary
key, student_fname varchar (30), student_lname varchar (30),
student_address varchar (30), city varchar(30), student_contact int);
 Question 03: Insert ten rows of data for the table.

insert into student


(student_id,student_fname,student_lname,student_address,city,student_contac
t) values ('st001','lakmal','Perera','no2 first street','Colombo',0771231234),
('st002','Suresh','Kumar','no2 first street','Colombo',0771781234),
('st003','Raj','Laxman','no2 second street','Kandy',0771231984),
('st004','Nipuni','Dinushika','no2 last street','Kalutara',0722231234),
('st005','lahiru','Sampath','no10 first street','Galle',0771671234),
('st006','Sidath','Dananjaya','no22 first street','Kurunagala',0781231234),
('st007','lakmal','Perera','no2 first street','Colombo',0721231234);
 Question 04: Select all the record from the table and display.
SELECT * FROM student;
 Question 05: Select student first name from table student. (5.1. Do the
same with distinct key)

SELECT student_fname FROM student;

SELECT distinct student_fname FROM from student;

Note: Distinct key word can be used to remove duplicate


 Question 06: Select all records of the student id 3.

SELECT * FROM student WHERE student_id = 'st003';

 Question 07: Select record of student address of student id is 4..

SELECT student_address FROM student WHERE student_id = 'st004';

 Question 08: Select record of student first name and student last name
where student id is 3.

SELECT student_fname,student_lname FROM student WHERE student_id =


'st003';
 Question 09: Select student first names between student id 2 to 5.

SELECT student_fname FROM student WHERE student_id BETWEEN 'st002' AND


'st005';

 Question 10 : Select all student last names starting with s%.

SELECT student_lname FROM student WHERE student_lname LIKE 's%';

 Question 11: Select record of student first name and student last name
where student id is 3.

SELECT student_fname, student_lname FROM student WHERE student_id =


'st003';

Note: - based on the position the percentage mark moves begin a% end %a
between %am%
 Question 12: Count the number of students in the table class.

SELECT COUNT(DISTINCT student_id) FROM student;

 Question 13 : Select all records from student where the city = Colombo
and last name = ‘Silva’

SELECT * FROM student WHERE city= 'Colombo' AND student_lname='Silva';

 Question 14: Select records from student table where city is not Kalutara

SELECT * FROM student WHERE NOT city='Kalutara';


 Question 14: Select all records from student where the city = ‘Colombo’
and city = ‘Kalutara’.

SELECT * FROM Student WHERE city='Colombo' or city='Kalutara';

 Question 15 : Select all records from student order by city in ascending order.
15.1 Select all records from student order by last name in descending order.

SELECT * FROM student ORDER BY city ASC;


SELECT * FROM student ORDER BY student_lname DESC;

 Question 16: Update table and change student last name to ‘'Fernando’ where
student id is st007.
UPDATE student SET student_lname='Fernando' WHERE student_id = 'st007';
 Question 17: update table and change student contact number to
‘0774124444’ where the student id is 1.

UPDATE student SET student_contact='0774124444' WHERE student_id = 'st001';

 Question 18 : Add a column called Status to student table

ALTER TABLE student ADD Status varchar(30) ;

 Question 19: Delete the student record of student id st007.

DELETE FROM student WHERE student_id='st007';


 Question 20: alter table drop column status.

ALTER TABLE student DROP COLUMN status;

 Question 21 : Find cities from student with ‘k’ and having 5 characters?
SELECT city FROM student WHERE city LIKE 'K____’;
Note: This is only to show after k there is 4 characters there should not be space in
between the underscore [‘K_ _ _ _ ‘]

 Question 22: Select student name having 2nd characters ‘i’ 4th character ‘u’ and
the character before the last is ‘k’ .

SELECT student_lname FROM student WHERE student_lname LIKE '_i_u%k_';


 Question 23 : Delete all student details of the student id between st003
to st006.

DELETE FROM student WHERE student_id BETWEEN 'st003' AND 'st006';

 Question 24 : Delete all from student.

DELETE FROM student;

Drop TABLE student; to delete the table


 Remove a table from the database (data and table structure)
 Drop table <table>
 Remove all data from the table
 Truncate table <table>
Table manipulation
 Change the name of a table
with SQL
 Rename <table> To <new table>
 Used to list all the field in a table and the data format of each field
 Describe <table>
 SELECT Max(quantity)
SELECT AVG(weight)
FROM part
Aggregate FROM part

values SELECT SUM(quentity)

functions FROM part

WHERE
student_number=”1”
 Question :Find average weight of Loan. [tbl - Loan]

SELECT AVG (weight) FROM Loan;

 Question : find the maximum quantity supplied. [tbl – shipment]

SELECT max(quantity) FROM shipment;

 Question : find the total quantity supplied by supplier 1. [tbl – shipment]

SELECT SUM(quantity) FROM shipment WHERE ship_id='s01';


 END

 https://www.youtube.com/watch?v=E-Fs49rIr7g
 https://www.microsoft.com/en-pk/download/details.aspx?
id=42299

You might also like