Lecture 04 - SQL Commands - DML
Lecture 04 - SQL Commands - DML
• 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?
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
• 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
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;