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

Lecture 04 - SQL Commands - DML

Uploaded by

talha.farooq6625
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture 04 - SQL Commands - DML

Uploaded by

talha.farooq6625
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

‫ْ‬ ‫َّ‬ ‫ٰ‬ ‫ْ‬ ‫َّ‬

‫هللا الرحمن الر ِحي ِم‬ ‫ْ‬


‫ِبس ِم ِ‬
‫ّس ل َأ ْمري َو ْ‬
‫اح ُللْ‬ ‫اش ْح ل َص ْدري َو َي ِّ ْ‬ ‫َر ِّب ْْ َ‬
‫َ ْ َ ُِ ي َْ‬ ‫َ‬ ‫ْ‬ ‫ً‬ ‫ي‬ ‫ِ‬ ‫ُْ َ‬
‫ان يفقهوا قو ِ يل‬ ‫عقدة ِمن ِلس ِ ي‬
SQL Commands - DML
Lecture # 4

Course Instructor: Adnan Rafique


Data Manipulation Language
(DML)
Data Manipulation Language (DML)
• DML is short name of Data Manipulation Language which deals with
data manipulation
• It includes most common SQL statements such SELECT, INSERT,
UPDATE, DELETE etc.
• It is used to store, modify, retrieve, delete and update data in
database.
DML-SQL SELECT Statement
• In SQL, the SELECT statement is used to retrieve data from a table in
the database. The returns data is stored in a table, and the result
table is known as result-set.
• Syntax
• SELECT column1, column2, ...columnN FROM table_name;

• Use the following syntax to select all the fields available in the table:
• SELECT * FROM table_name;
Write an sql query to fetch all the employee’s id?

Select emp_id from employee;


Write an sql query to fetch employee names and salaries of all
employees?
SELECT EMP_NAME, SALARY FROM EMPLOYEE;
Movie table
Basic SQL Statements
• The basic SQL statements are as follows:
• Select Statement
• Select statement is used to select data from a table. It displays result in
tabular form.
• Syntax
• Select column_names(s) From table_name;
• Select One Column
• Write a query that display MovieID from Movie table?
Select MovieID from Movie;
Select Multiple Columns
• Syntax:
Select ColumnName1,ColumnName2… from TableName;
• Write a query that displays the columns MovieTitle, MovieReleasedDate and
MovieRating?
Select MovieTitle, MovieReleasedDate,MovieRating from Movie;
Select All Columns
• Write a query that displays all columns from Movie?
Select * from Movie;
Here * symbol is used instead of column names to display all columns of a table.
Select statement with where clause
Where clause is used to retrieve data from a table conditionally. Or
Where clause is used to filter records. It can appear only after from
clause.
• Syntax
Select ColumnName1, ColumnName2, ColumnName3… from Table Name
where condition;
Or
Select * from Table Name where condition;
Where clause
• Write a query to display movie id, movie title, movie released date,
movie budget, country, movie rating and movie sold having movie
title ‘Kung Fu Panda 2’?
Select * from Movie where MovieTitle='Kung Fu Panda 2';
Customer Table
Data Types int varchar varchar varchar varchar varchar varchar
SQL INSERT INTO Statement
• The Insert into statement is used to insert new records in a table.
• Insert Into Syntax
• It is possible to write the INSERT INTO statement in two ways:
• The first way specifies both the column names and the values to be
inserted:
• INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2,
value3, ...);
• If you are adding values for all the columns of the table, you do not need to
specify the column names in the SQL query. However, make sure the order
of the values is in the same order as the columns in the table. The INSERT
INTO syntax would be as follows:
• INSERT INTO table_name VALUES (value1, value2, value3, ...);
SQL INSERT INTO Statement
Write a SQL query to insert any new record into Customer table.
INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');

New record

Note: Here we have inserted record in columns CustomerName, City, Country only. The values will be initialized
with these values as given in Insert Into statement. In the remaining columns null value will be initialized.
CustomerID is Auto Number it will be incremented by 1.
SQL Update Statement
• The Update statement is used to modify the existing records in a table.
• Update Syntax
• Update table_name set column1 = value1, column2 = value2, ...
where condition;
• Update customers set contactname = 'Alfred Schmidt', city= 'Frankfurt'
where customerid = 1;
Updated
Update All records
• Be careful when updating records. If you omit the Where clause, all
records will be updated. In this update statement where
clause is omitted so ContactName
• Update Customers Set ContactName='Juan'; attribute of all records will be
updated.
SQL DELETE Statement
• The DELETE statement is used to delete existing records in a table.
• Delete Syntax
• Delete from table_name where condition;
SQL DELETE Example
• The following SQL statement deletes the customer "Alfreds
Futterkiste" from the "Customers" table:
• Example
• Delete from Customers where CustomerID=1;
Delete All Records
• It is possible to delete all rows in a table without deleting the table.
This means that the table structure, attributes, and indexes will be
intact:
• Delete from table_name;
• The following SQL statement deletes all rows in the "Customers"
table, without deleting the table:
• Example
• Delete from Customers;
22

SELECT Statement-More examples


Employee Table
ID Name Age Address Salary
1 Zeeshan 32 CII Johar Town 2000
2 Ali 25 J2 Block Wapda 1500
Town
3 Rizvi 23 111 Green Town 2000
4 Hamza 25 114 Minhas Colony 6500
5 Yasir 27 233 Shah Jamal 8500
6 Asif 22 156 PGECHS Phase 2 4500
7 Hammad 24 14 A Nespak Society 10000
23

SELECT Statement
• SELECT ID, NAME, SALARY FROM Employee;
• Result
ID Name Salary
1 Zeeshan 2000
2 Ali 1500
3 Rizvi 2000
4 Hamza 6500
5 Yasir 8500
6 Asif 4500
7 Hammad 10000
25

WHERE with SELECT


• To limit the number of rows use the WHERE clause.
• The WHERE clause filters for rows that meet certain
criteria.
• WHERE is used with SELECT, UPDATE, and DELETE.

SELECT column-names
FROM table-name
WHERE condition
26

Where Statement
Write an SQL query to show those employees id, name, age
and salary whose monthly salary is greater than 2000.
Table: Emp
ID Name Age Address Salary
1 Zeeshan 32 CII Johar Town 2000
2 Ali 25 J2 Block Wapda 1500
Town
3 Rizvi 23 111 Green Town 2000
4 Hamza 25 114 Minhas Colony 6500
5 Yasir 27 233 Shah Jamal 8500
6 Asif 22 156 PGECHS Phase 2 4500
7 Hammad 24 14 A Nespak Society 10000
27

Where Statement
ID Name Age Salary
4 Hamza 25 6500
5 Yasir 27 8500
6 Asif 22 4500
7 Hammad 24 10000
28

Where
SELECT ID, NAME, Age, SALARY FROM
Emp WHERE SALARY > 2000;

You might also like