SQL Queries
What is SQL?
SQL stands for Structured Query Language.
SQL lets you access and manipulate databases.
SQL is an ANSI (American National Standards Institute) standard.
SQL statements categories
SQL language is divided into four types of primary language statements: DML, DDL, DCL
and TCL.
The four main categories of SQL statements are as follows:
1. DML (Data Manipulation Language)
2. DDL (Data Definition Language)
3. DCL (Data Control Language)
4. TCL (Transaction Control Language)
DML (Data Manipulation Language)
DML statements affect records in a table.
These are basic operations we perform on data such as selecting a few records from a
table, inserting new records, deleting unnecessary records, and updating/modifying
existing records.
DML statements include the following:
SELECT: select records from a table.
INSERT: insert new records.
UPDATE: update/Modify existing records.
DELETE: delete existing records.
DDL (Data Definition Language)
DDL statements are used to alter/modify a database or table structure and schema.
These statements handle the design and storage of database objects.
DDL statements include the following:
CREATE – create a new Table, database, schema.
ALTER – alter existing table, column description.
DROP – delete existing objects from database.
DCL (Data Control Language)
DCL statements control the level of access that users have on database objects.
DCL statements include the following:
GRANT: allows users to read/write on certain database objects.
REVOKE: keeps users from read/write permission on database objects.
TCL (Transaction Control Language)
TCL statements allow you to control and manage transactions to maintain the integrity of
data within SQL statements.
TCL statements include the following:
BEGIN Transaction: opens a transaction.
COMMIT Transaction – commits a transaction.
ROLLBACK Transaction – ROLLBACK a transaction in case of any error.
What Can SQL do?
SQL can execute queries against a database
SQL can retrieve data from a database
SQL can insert records in a database
SQL can update/delete records in a database
SQL can create new databases
SQL can create new tables in a database
SQL can create stored procedures in a database
SQL can create views in a database
SQL can set permissions on tables, procedures, and views.
SELECT Statement
The SELECT statement is used to select data from a database
Syntax:
SELECT column1, column2, ...
FROM table_name;
Example:
SELECT empID, empName FROM emp;
SELECT DISTINCT Statement
The SELECT DISTINCT statement is used to return only distinct (different) values.
Syntax:
SELECT DISTINCT colum_name
FROM table_name;
Example:
SELECT DISTINCT DNAME FROM EMP;
SELECT COUNT ( DISTINCT DNAME) FROM EMP;
SQL WHERE Clause
The WHERE clause is used to filter records. The WHERE clause is used to extract only those
records that fulfill a specified condition.
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
SELECT * FROM EMP WHERE LOC=‘NEW YORK';
SELECT * FROM EMP WHERE EMPNO=7876;
AND Operator
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
Example:
SELECT * FROM EMP
WHERE LOC=‘NEW YORK' AND EMPNO=7782;
OR Operator
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
Example:
SELECT * FROM Customers
WHERE LOC=‘NEW YORK' OR LOC=‘CHICAGO';
NOT Operator
Syntax :
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
Example:
SELECT * FROM EMP
WHERE NOT JOB=‘CLERK';
Operators in The WHERE Clause
= Equal
> 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
BETWEEN Operator
The BETWEEN operator selects values within a given range. The values can be
numbers, text, or dates
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Example:
SELECT * FROM EMP WHERE EMPNO BETWEEN 7521 AND 7698;
BETWEEN (Text Values)
Example:
SELECT * FROM EMP
WHERE LOC BETWEEN 'CHICAGO’
AND ‘NEW YORK’ ;
Example:
SELECT * FROM EMP
WHERE JOB NOT BETWEEN ‘ANALYST’
AND ‘CLERK’ ;
BETWEEN (Date)
Example1:
SELECT * FROM emp
WHERE hiredate BETWEEN TO_DATE ( ‘07/04/1980’ , ‘DD/MM/YYYY’ )
AND TO-DATE ( ‘07/09/1985’ , ‘DD/MM/YYYY’ );
Example2:
SELECT * FROM emp
WHERE hiredate NOT BETWEEN TO_DATE ( ‘07/04/1980’ , ‘DD/MM/YYYY’ )
AND TO-DATE ( ‘07/09/1985’ , ‘DD/MM/YYYY’ );
IN Operator
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
OR
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
IN Operator
Example1
SELECT * FROM EMP
WHERE JOB IN (‘NEW YORK’, ‘CHICAGO’ );
SELECT * FROM EMP
WHERE EMPNO IN (7876, 7499, 7698);
Example2
SELECT * FROM EMP
WHERE JOB NOT IN (‘NEW YORK’, ‘CHICAGO’ );
LIKE Operator
The LIKE operator is used in a WHERE clause to search for a specified pattern in a
column.
LIKE operator used in two different ways
% The percent sign represents zero, one, or multiple characters.
_ The underscore represents a single character.
LIKE Operator
LIKE Operator Description
WHERE ENAME LIKE ‘A%' Finds any values that starts with ‘A’.
WHERE ENAME LIKE '%A' Finds any values that ends with ‘A’.
WHERE ENAME LIKE ‘%M%' Finds any values that have ‘M’ in any position.
WHERE ENAME LIKE '_A%' Finds any values that have ‘A’ in the second position.
WHERE ENAME LIKE ‘A%O' Finds any values that starts with ‘A’ and ends with ‘O’.
LIKE Operator
SELECT * FROM EMP WHERE ENAME LIKE ‘A%';
SELECT * FROM EMP WHERE ENAME LIKE '%A’;
SELECT * FROM EMP WHERE ENAME LIKE '%M%’;
SELECT * FROM EMP WHERE ENAME LIKE '_A%’;
SELECT * FROM EMP WHERE ENAME LIKE ‘A%O’;
COUNT() Function
Syntax:
SELECT COUNT (column_name)
FROM table_name
WHERE condition;
Example
SELECT COUNT ( DISTINCT DNAME )
FROM EMP;
AVG() Functions
Syntax:
SELECT AVG (column_name)
FROM table_name
WHERE condition;
Example:
SELECT AVG (Price)
FROM Products;
SUM() Function
Syntax:
SELECT SUM (column_name)
FROM table_name
WHERE condition;
Example
SELECT SUM (Quantity)
FROM OrderDetails;
MIN() Function
Syntax:
SELECT MIN (column_name)
FROM table_name
WHERE condition;
Example:
SELECT MIN PRICE AS SmallestPrice
FROM Products;
MAX() Functions
Syntax:
SELECT MAX (column_name)
FROM table_name
WHERE condition;
Example
SELECT MAX (Price) AS LargestPrice
FROM Products;
ORDER BY Clause
Syntax:
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
Example
SELECT * FROM Customers
ORDER BY Country;
SELECT * FROM Customers
ORDER BY Country DESC;
ORDER BY Clause
Example
SELECT * FROM Customers
ORDER BY Country, CustomerName;
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;
UPDATE Table
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example
UPDATE Customers
SET CustomerName = 'Ahmad', City= ‘LHR'
WHERE CustomerID = 1;
DELETE Statement
Syntax:
DELETE FROM table_name
WHERE condition;
Example
DELETE FROM Customers
WHERE CustomerName=‘Ahmad’;
DELETE * FROM table_name;
IS NULL Operator
Syntax:
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
Example
SELECT LastName, FirstName, Address FROM Persons
WHERE Address IS NULL;
IS NOT NULL Operator
Syntax:
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
Example
SELECT LastName, FirstName, Address FROM Persons
WHERE Address IS NOT NULL;
SQL Aliases
Syntax:
SELECT column_name AS alias_name
FROM table_name;
Example
SELECT CustomerID AS ID, CustomerName AS Customer
FROM Customers;
SELECT CustomerName, Address + ', ' + PostalCode + ' ' + City + ', ' +
Country AS Address FROM Customers;
GROUP BY Statement
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);
GROUP BY Statement
Example:
SELECT COUNT (CustomerID), Country
FROM Customers
GROUP BY Country;
GROUP BY Statement
The HAVING clause was added to SQL because the WHERE keyword could not be used
with aggregate functions.
Example:
SELECT COUNT (CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT (CustomerID) > 7;
SQL HAVING
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;