You are on page 1of 33

SQL DML

Insert- Update - Delete


INSERT INTO table_name (column1, column2
, column3, ...)
VALUES (value1, ’value2’, value3, ...);
Insert a record
in all columns • Example:
INSERT INTO Customers (c_ID,CustomerName, ContactName,
Address, City, PostalCode, Country)
VALUES (1,'Cardinal', 'Tom B. Erichsen', 'Skagen
21', 'Stavanger', '4006', 'Norway');
Insert a record
in specific CustomerID CustomerName ContactName Address City PostalCode Country

columns 89 White Clover


Markets
Karl Jablonski 305 - 14th
Ave. S.
Seattle 98128 USA

Suite 3B

90 Wilman Kala Matti Karttunen Keskuskatu Helsinki 21240 Finland


45
• INSERT INTO Customers
(CustomerName, City, 91 Wolski Zbyszek ul. Filtrowa Walla 01-012 Poland
68
Country)
VALUES ('Cardinal', 'Stava 92 Cardinal null null Stavanger null Norway
nger', 'Norway');
• UPDATE table_name
SET column1 = value1, column2 = value2,
Update a ...
record WHERE condition;
• Example:
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
CustomerID CustomerNa ContactName Address City PostalCode Country
me
Update 1 Alfreds Alfred Obere Str. 57 Frankfurt 12209 Germany

multiple
Futterkiste Schmidt

records 2 Ana Trujillo Juan


Emparedad
Avda. de la
Constitución
México D.F. 05021 Mexico
os y helados 2222

UPDATE Customers 3 Antonio Juan Mataderos México D.F. 05023 Mexico


Moreno 2312
SET ContactName='Juan' Taquería

WHERE Country='Mexico'; 4 Around the


Horn
Thomas Hardy 120 Hanover
Sq.
London WA1 1DP UK

5 Berglunds Christina Berguvsväge Luleå S-958 22 Sweden


snabbköp Berglund n8
• DELETE FROM table_name WHERE condition;
Deleting • Example:
records DELETE FROM Customers WHERE CustomerNa
me='Alfreds Futterkiste';
• DELETE FROM table_name;
Deleting all • Example:
records DELETE FROM Customers;
• The SELECT statement is used to select data
from a database.
• The data returned is stored in a result table,
Select called the result-set.
statement
SELECT column1, column2, ...
FROM table_name;
SELECT * FROM table_name;

The SQL SELECT DISTINCT Statement

Select all The SELECT DISTINCT statement is used to return only


and select distinct (different) values.

distinct Inside a table, a column often contains many duplicate


values; and sometimes you only want to list the different
(distinct) values.
SELECT DISTINCT column1, column2, ...
FROM table_name;
Example

SELECT Country FROM Customers; SELECT DISTINCT Country FROM Customers;


Select
conditions
SELECT * FROM Customers
WHERE CustomerID=1;

SELECT Id, ProductName, UnitPrice


FROM Product
Select WHERE UnitPrice IN (10,20,30,40,50);
conditions
examples SELECT * FROM Customers
WHERE Country='Mexico’;
Like clause
Like clause
Is null
Is not null
And
Or
Not
And operator
And operator
Not operator
Example
Example Like
and Or
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

SELECT * FROM Customers


ORDER BY Country;

Order by SELECT * FROM Customers


ORDER BY Country DESC;
Group by
having
What does the HAVING clause do in a query?

The HAVING clause is like WHERE but


operates on grouped records returned by a
GROUP BY.
Group by HAVING applies to summarized group records,
whereas WHERE applies to individual records.
having Only the groups that meet the HAVING criteria
will be returned.
HAVING requires that a GROUP BY clause is
present.
Both WHERE and HAVING can be used in the
same query at the same time.
Group by
having
Group by
having
Group by
having
• An Alias is a shorthand for a table or column
name.
Aliases reduce the amount of typing required to
enter a query.
• Complex queries with Aliases are generally easier
to read.
Aliases are useful with JOINs and aggregates:
SQL Alias SUM, COUNT, etc.
An Alias only exists for the duration of the query.

Syntax:
SELECT column-name AS alias-name
FROM table-name alias-name
WHERE condition
Alias
The GROUP BY clause groups records into summary rows.
It returns one record for each group.
GROUP BY queries often include aggregates: COUNT, MAX,
SUM, AVG, etc.
A GROUP BY clause can group by one or more columns.

Group by SELECT column-names


having FROM table-name
WHERE condition
GROUP BY column-names
Write an SQL query to print details of workers excluding first names,
“Vipul” and “Satish” from Worker table.

The required query is:


Select * from Worker where FIRST_NAME not in ('Vipul','Satish’);
SQL DML Write an SQL query to print details of Workers with DEPARTMENT
exercises name as “Admin”.
Ans.
The required query is:
Select * from Worker where DEPARTMENT like 'Admin%';

Write an SQL query to print details of the Workers whose FIRST_NAME


contains ‘a’.
Ans.
The required query is:
Select * from Worker where FIRST_NAME like '%a%';
Write an SQL query to fetch the no. of workers for each department in
the descending order.
Ans.
The required query is:
SELECT DEPARTMENT, count(WORKER_ID) No_Of_Workers FROM
worker GROUP BY DEPARTMENT ORDER BY No_Of_Workers DESC;
SQL DML Write an SQL query to fetch the list of employees with the same
exercises salary.
Ans.
The required query is:
Select distinct W.WORKER_ID, W.FIRST_NAME, W.Salary
from Worker W, Worker W1
where W.Salary = W1.Salary and W.WORKER_ID != W1.WORKER_ID;

Write an SQL query to show the second highest salary from a table.
Ans.
The required query is:
Select max(Salary) from Worker where Salary not in (Select max(Salary)
from Worker);

You might also like