6 SQL DML
6 SQL DML
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
UPDATE Statement
UPDATE Student
SET City= ‘Pune',
WHERE RollNo = 1;
UPDATE Multiple Records
UPDATE Student
SET City=‘Pune';
DELETE Statement
SELECT * Example
The SQL statement to select all the columns
from the “Student" table:
Examples
The SQL statement selects only the DISTINCT
values from the "Country" column in the
“Student" table:
SELECT DISTINCT Country FROM Student;
WHERE Clause
Operator Description
= Equal
<> Not equal. Note: In some versions of SQL this operator may be written as
!=
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN To specify multiple possible values for a column
SQL AND, OR and NOT Operators
The WHERE clause can be combined with AND,
OR, and NOT operators.
The AND and OR operators are used to filter
records based on more than one condition.
The AND operator displays a record if all the
conditions separated by AND is TRUE.
The OR operator displays a record if any of the
conditions separated by OR is TRUE.
The NOT operator displays a record if the
condition(s) is NOT TRUE.
SQL AND, OR and NOT Operators
AND Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
OR Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
AND Example
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
IN Operator
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT)
;
IN Operator
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
BETWEEN Operator
SELECT MIN(column_name)
FROM table_name
WHERE condition;
SELECT MAX(column_name)
FROM table_name
WHERE condition;
SQL MIN() and MAX() Functions
SELECT AVG(column_name)
FROM table_name
WHERE condition;
SELECT SUM(column_name)
FROM table_name
WHERE condition;
COUNT()
• Finds the number of products
SELECT COUNT(ProductID)
FROM Products;
SELECT AVG(Price)
FROM Products;
SUM()
SELECT SUM(Quantity)
FROM OrderDetails;
GROUP BY Statement
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
SQL GROUP BY Examples
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
SQL HAVING Examples