Structured Query Language Tutorial
Structured Query Language Tutorial
What is SQL?
A database most often contains one or more tables. Each table is identified
by a name (e.g. "Customers" or "Orders"). Tables contain records (rows)
with data.
The table above contains three records (one for each person) and four
columns (LastName, FirstName, Address, and City).
SQL Queries
With SQL, we can query a database and have a result set returned.
A query like this:
LastName
AAA
CCC
EEE
SQL Data Manipulation Language (DML)
SQL (Structured Query Language) is a syntax for executing queries. But the
SQL language also includes a syntax to update, insert, and delete records.
These query and update commands together form the Data Manipulation
Language (DML) part of SQL:
The Data Definition Language (DDL) part of SQL permits database tables to
be created or deleted. We can also define indexes (keys), specify links
between tables, and impose constraints between database tables.
Syntax
SELECT column_name(s) FROM table_name
Syntax
SELECT DISTINCT column_name(s) FROM table_name
Syntax
SELECT column FROM table WHERE column operator value With
Using Quotes
SQL uses single quotes around text values (most database systems will also
accept double quotes). Numeric values should not be enclosed in quotes.
Syntax
SELECT column FROM table WHERE column LIKE pattern
Using LIKE
The following SQL statement will return persons with first names that start
with an 'A':
The following SQL statement will return persons with first names that end
with an 'A':
Syntax
INSERT INTO table_name VALUES (value1, value2,.. .)
You can also specify the columns for which you want to insert data:
INSERT INTO table_name (column1, column2,...)
VALUES (value1, value2,. . .)
Syntax
UPDATE table_name
SET column_name = new_value
WHERE column_name = some_value
Syntax
DELETE FROM table_name
WHERE column_name = some_value
AND & OR
AND and OR join two or more conditions in a WHERE clause.
The AND operator displays a row if ALL conditions listed are true. The OR
operator displays a row if ANY of the conditions listed are true.
Syntax
Use AND to display each person with the first name equal to "AAA", and the
last name equal to "BBB":
SELECT * FROM Persons
WHERE FirstName='AAA'
AND LastName='BBB'
Syntax
Use OR to display each person with the first name equal to "AAA", or the last
name equal to "BBB":
SELECT * FROM Persons
WHERE firstname='AAA'
OR lastname='BBB'
IN
The IN operator may be used if you know the exact value you want to return
for at least one of the columns.
ALIASES
With SQL, aliases can be used for column names and table names.
Column Name Alias
The syntax is:
Joins
Sometimes we have to select data from two or more tables to make our
result complete. We have to perform a join.
Tables in a database can be related to each other with keys. A primary key is
a column with a unique value for each row. The purpose is to bind data
together, across tables, without repeating all of the data in every table.
Employees:
Employee_ID Name
01 AAA, BBB
02 CCC, DDD
03 EEE, FFF
04 GGG, HHH
Orders:
Prod_ID Product Employee_ID
111 Printer 01
112 Monitor 03
113 Mouse 03
114 Keyboard 04
Example
Who ordered a keyboard?
SELECT Employees.Name
FROM Employees, Orders
WHERE Employees.Employee_ID=Orders.Employee_ID
AND Orders.Product='Keyboard'
Types of Functions
There are several basic types and categories of functions in SQL. The basic
types of functions are:
Aggregate Functions
Scalar functions
Aggregate functions
Aggregate functions operate against a collection of values, but return a single
value. When used among many other expressions in the item list of a SELECT
statement, the SELECT must have a GROUP BY clause.
GROUP BY...
GROUP BY... was added to SQL because aggregate functions (like SUM)
return the aggregate of all column values every time they are called, and
without the GROUP BY function it was impossible to find the sum for each
individual group of column values.
HAVING...
HAVING... was added to SQL because the WHERE keyword could not be used
against aggregate functions (like SUM), and without HAVING... it would be
impossible to test for result conditions.