UNIT3 Parti
UNIT3 Parti
Today, SQL is mostly used by programmers who use SQL inside their language to
build applications that access data in a database.
Four fundamental operations that apply to any database are:
1. Read the data -- SELECT
2. Insert new data -- INSERT
3. Update existing data -- UPDATE
4. Remove data – DELETE
Collectively these are referred to as CRUD (Create, Read, Update, Delete). The
general form for each of these 4 operations in SQL is:
The SQL SELECT general form
SELECT column-names
FROM table-name
WHERE condition
ORDER BY sort-order;
Example:
SELECT FirstName, LastName, City, Country
FROM Customer
WHERE City = 'Paris'
ORDER BY LastName;
Example:
INSERT into Supplier (Name, ContactName, City, Country)
VALUES ('Oxford Trading', 'Ian Smith', 'Oxford', 'UK');
Example:
DELETE from Customer
WHERE Email = '[email protected]';
UPDATE table-name
SET column-name = value
WHERE condition;
A WHERE clause with a DELETE statement:
DELETE FROM table-name
WHERE condition;
The ALTER TABLE statement is also used to add and drop various constraints on
an existing table.
AGGREGATE FUNCTIONS
Problem: List all products that are not exactly $10, $20, $40, or $50
unitprice.
SELECT Id, ProductName, UnitPrice
FROM Product
WHERE UnitPrice NOT IN (10,20,40,50);
Problem: List all customers that are from the same countries as the
suppliers.
SELECT Id, FirstName, LastName, Country
FROM Customer
WHERE Country IN (SELECT Country FROM Supplier);
SQL WHERE LIKE Statement:
The LIKE operator is used in a WHERE clause to search for a specified pattern in
a column.
There are two wildcards often used in conjunction with the LIKE operator:
% - The percent sign represents zero, one, or multiple characters.
_ - The underscore represents a single character.
The SQL WHERE LIKE syntax
The general syntax is:
SELECT column-names
FROM table-name
WHERE column-name LIKE value;
SQL WHERE LIKE Examples
Problem: List all products with names that start with
'Ca'.SELECT Id, ProductName, UnitPrice,
Package
FROM Product
WHERE ProductName LIKE 'Ca%';
Problem: List all products that start with 'Cha' or 'Chan' and have one
morecharacter.
SELECT Id, ProductName, UnitPrice, Package
FROM Product
WHERE ProductName LIKE 'Cha_' OR ProductName LIKE 'Chan_';
Here are some examples showing different LIKE operators with '%' and '_'
wildcards:
SQL IS NULL Clause:
What is a NULL Value?
A field with a NULL value is a field with no value.
If a field in a table is optional, it is possible to insert a new record or update
a record without adding a value to this field. Then, the field will be saved
with a NULL value.
Note: A NULL value is different from a zero value or a field that contains
spaces.A field with a NULL value is one that has been left blank during record
creation!
Problem: List the number of customers in each country, except the USA,
sortedhigh to low. Only include countries with 9 or more customers.
SELECT COUNT(Id), Country
FROM Customer
WHERE Country <> 'USA'
GROUP BY Country
HAVING COUNT(Id) >= 9
ORDER BY COUNT(Id) DESC;
Problem: List all customer with average orders between $1000 and $1200.
SELECT AVG(TotalAmount), FirstName, LastName
FROM [Order] O JOIN Customer C ON O.CustomerId = C.Id
GROUP BY FirstName, LastName
HAVING AVG(TotalAmount) BETWEEN 1000 AND 1200;
SQL Alias:
SQL aliases are used to give a table, or a column in a table, a temporary
name.
Aliases are often used to make column names more readable.
An alias only exists for the duration of the query.
The SQL Alias syntax
The general syntax is:
SELECT column-name AS alias-name
FROM table-name alias-name
WHERE condition;