SQL Examples

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

[SELECT]

The SELECT statement is used to select data from a database.

SYNTAX:
SELECT column1, column2, ... FROM table_name;

EXAMPLES:
SELECT * FROM table_name;
SELECT CustomerName, City, Country FROM Customers;

[SELECT FUNCTION()]
The SELECT DISTINCT statement is used to return only distinct (different) values.
The MIN() function returns the smallest value of the selected column.
The MAX() function returns the largest value of the selected column.
The COUNT() function returns the number of rows that matches a specified criterion.
The AVG() function returns the average value of a numeric column.
The SUM() function returns the total sum of a numeric column.

SYNTAX:
SELECT DISTINCT column1, column2, ... FROM table_name;
SELECT MIN(column_name) FROM table_name WHERE condition;
SELECT MAX(column_name) FROM table_name WHERE condition;
SELECT COUNT(column_name) FROM table_name WHERE condition;
SELECT AVG(column_name) FROM table_name WHERE condition;
SELECT SUM(column_name) FROM table_name WHERE condition;

EXAMPLES:
SELECT DISTINCT Country FROM Customers;
SELECT COUNT(DISTINCT Country) FROM Customers;
SELECT MIN(Price) AS SmallestPrice FROM Products;
SELECT MAX(Price) AS LargestPrice FROM Products;
SELECT COUNT(ProductID) FROM Products;
SELECT AVG(Price) FROM Products;
SELECT SUM(Quantity) FROM OrderDetails;

[LIKE OPERATOR]
The LIKE operator is used in a WHERE clause to search for a specified pattern in a
column.

SYNTAX:
SELECT column1, column2, ... FROM table_name WHERE columnN LIKE pattern;

EXAMPLES:
SELECT * FROM Customers WHERE CustomerName LIKE 'a%';
SELECT * FROM Customers WHERE CustomerName LIKE '%a';
SELECT * FROM Customers WHERE CustomerName LIKE '%or%';
SELECT * FROM Customers WHERE CustomerName LIKE '_r%';
SELECT * FROM Customers WHERE CustomerName LIKE 'a__%';
SELECT * FROM Customers WHERE ContactName LIKE 'a%o';
SELECT * FROM Customers WHERE CustomerName NOT LIKE 'a%';

[WHERE]
The WHERE clause is used to filter records.

SYNTAX:
SELECT column1, column2, ... FROM table_name WHERE condition;

EXAMPLES:
SELECT * FROM Customers WHERE Country = 'Mexico';
SELECT * FROM Customers WHERE CustomerID = 1;
SELECT * FROM Customers WHERE Country = 'Germany' AND City = 'Berlin';
SELECT * FROM Customers WHERE City = 'Berlin' OR City = 'Stuttgart';
SELECT * FROM Customers WHERE NOT Country = 'Germany';
SELECT * FROM Customers WHERE Country = 'Germany' AND (City = 'Berlin' OR City =
'Stuttgart');
SELECT * FROM Customers WHERE NOT Country = 'Germany' AND NOT Country = 'USA';

SELECT CustomerName, ContactName, Address FROM Customers WHERE Address IS NULL;


SELECT CustomerName, ContactName, Address FROM Customers WHERE Address IS NOT NULL;

[ORDER BY]
The ORDER BY keyword is used to sort the result-set in ascending or descending
order.

SYNTAX:
SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ... ASC|
DESC;

EXAMPLES:
SELECT * FROM Customers ORDER BY Country;
SELECT * FROM Customers ORDER BY Country DESC;
SELECT * FROM Customers ORDER BY Country, CustomerName;
SELECT * FROM Customers ORDER BY Country ASC, CustomerName DESC;

[GROUP BY]
The GROUP BY statement groups rows that have the same values into summary rows,
like "find the number of customers in each country". The GROUP BY statement is
often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group
the result-set by one or more columns.
SYNTAX:
SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s) ORDER
BY column_name(s);

EXAMPLES:
SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country;
SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country ORDER BY
COUNT(CustomerID) DESC;

[HAVING]
The HAVING clause was added to SQL because the WHERE keyword cannot be used with
aggregate functions.

SYNTAX:
SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s)
HAVING condition ORDER BY column_name(s);

EXAMPLES:
SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country HAVING
COUNT(CustomerID) > 5;
SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country HAVING
COUNT(CustomerID) > 5 ORDER BY COUNT(CustomerID) DESC;

[INSERT INTO]
The INSERT INTO statement is used to insert new records in a table.

SYNTAX:
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2,
value3, ...);
INSERT INTO table_name VALUES (value1, value2, value3, ...);

EXAMPLES:
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode,
Country) VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006',
'Norway');
INSERT INTO Customers (CustomerName, City, Country) VALUES ('Cardinal',
'Stavanger', 'Norway');

[UPDATE]
The UPDATE statement is used to modify the existing records in a table.
SYNTAX:
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;

EXAMPLES:
UPDATE Customers SET ContactName = 'Alfred Schmidt', City = 'Frankfurt' WHERE
CustomerID = 1;
UPDATE Customers SET PostalCode = 00000 WHERE Country = 'Mexico';
UPDATE Customers SET PostalCode = 00000;

[DELETE]
The DELETE statement is used to delete existing records in a table.

SYNTAX:
DELETE FROM table_name WHERE condition;

EXAMPLES:
DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';
DELETE FROM Customers;

You might also like