This document provides an introduction to structured query language (SQL). It describes SQL's use for communicating with databases and its basis in set theory and relational operations. Examples are provided to demonstrate basic SQL statements like SELECT, FROM, WHERE, DISTINCT, ORDER BY, LIKE, IN, BETWEEN and how to retrieve, filter and sort data from database tables. Keywords, operators and syntax are defined for core SQL clauses and functions.
2. SQL
SQL stands for Structured Query Language. SQL is used to
communicate with a database. According to ANSI (American National
Standards Institute), it is the standard language for relational
database management systems.
SQL is based on set and relational operations with certain
modifications and enhancements
3. SQL select statement
retrieving data from the database by specifying which columns and rows to be
retrieved
The basics syntax for select statement is :
SELECT column1, column2, …….
FROM table
SELECT column1, column2, …….
FROM table
6. SQL SELECT DISTINCT Statement
The SELECT DISTINCT statement is used to return only distinct (different)
values.
Inside a table, a column often contains many duplicate values; and sometimes
you only want to list the different (distinct) values.
SELECT DISTINCT Syntax
SELECT DISTINCT column1, column2, ...
FROM table_name;
8. WHERE
Commonly 3 clauses (SELECT, FROM, WHERE) are used
Query: List the names of the capital cities in the CITY
table.
9.
10. SQL and, or Syntax
The syntax for and condition is:
The syntax for and condition is:
SELECT column1, column2, …….
FROM table
WHERE condition1 AND condition2 AND condition3 ...;
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
13. Examples
SELECT * FROM Customers
WHERE Country='Germany' AND City='Berlin';
SELECT * FROM Customers
WHERE Country='Germany' AND (City='Berlin' OR City='München');
14. SQL ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set in ascending or
descending order.
The ORDER BY keyword sorts the records in ascending order by default. To
sort the records in descending order, use the DESC keyword.
ORDER BY Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
16. 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;
17. SQL LIKE Operator
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
LIKE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
21. SQL IN Operator
The IN operator allows you to specify multiple values in a WHERE clause.
The IN operator is a shorthand for multiple OR conditions.
IN 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);
22. SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');
23. SELECT * FROM Customers
WHERE Country NOT IN ('Germany', 'France', 'UK');
24. BETWEEN Operator
The BETWEEN operator selects values within a given range. The values can be
numbers, text, or dates.
The BETWEEN operator is inclusive: begin and end values are included.
BETWEEN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
26. Examples
Employee (Ename,DepartmentID,Salary)
Department(DepartmentID, DepartmentName)
1- Select all employees with EName that starts with “R”
Ans:
SELECT * FROM Employees WHERE EName LIKE ’R%’
2- Select all employees with EName that contains “R”
Ans:
SELECT * FROM Employees WHERE EName LIKE ’%R%’
3- Select all students with Sname ending with “J”
Ans:
SELECT * FROM Employees WHERE EName LIKE ’%J’
27. Examples
4- Select employees in departments number 31,34 and 35
Ans:
SELECT * FROM Employees WHERE DepartmentId IN (31,34,35)
5- Select employees that works in a department
Ans:
SELECT * FROM Employees WHERE DepartmentId IN (SELECT DepartmentID FROM
Department)
6-Select employees that earn salaries between 1000 and 2000 dollar
Ans:
Select * from Employees WHERE Salary BETWEEN 1000 AND 2000