Vous êtes sur la page 1sur 46

Introduction to SQL

Prepared by: Romer Ian O.


Pasoc

What is SQL?
SQL stands for Structured Query Language.
It is the most widely used commercial
relational database language.
Now the de facto standard for DBMS
products by ANSI (American National
Standards Institute).
SQL continues to evolve in response to
changing need in the database era.

What is SQL
SQL lets you access and manipulate
databases

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 records in a database;
SQL can delete records from a database;
SQL can create new databases;
SQL can create new tables in a database;

Some of the Most Important


SQL Commands

SELECT
UPDATE
DELETE
INSERT INTO
CREATE DATABASE
ALTER DATABASE
CREATE TABLE
ALTER TABLE
DROP TABLE

SQL Functions
DDL (Data Definition Language)
Function of SQL that supports the creation,
deletion, and modification of definitions for
tables. The DDL also provides commands for
specifying access rights or privileges to
tables.
DML (Data Manipulation Language)
Function of SQL that allows users to pose
queries and to insert, delete, and modify
rows/records.

Data Definition
Creation of the schema or overall structure
of the database.
Two task must be completed:
-Create database structure
-Create tables that will hold end-user data

The Database Schema


AUTHENTICATION
- DBMS verifies that only registered users are
able to access database
- Log on using the user ID and password
created by database administrator
SCHEMA
- Group of database objects that are related to
each other

DDL Commands
CREATE
The CREATE command is used to establish
databases and tables in your DBMS.
ALTER
Once you have created a table within a
database, you may wish to modify the
definition of it. The ALTER command allows
you to make changes to the structure of a
table without deleting and recreating it.

DDL Commands
DROP
The final command of the DDL, DROP,
allows us to remove entire database
object (table or database) from the DBMS.

Data Manipulation
These commands will be used by all
database users during the routine operation
of the database.

DML Commands
SELECT
The SELECT command is the most
commonly used command in SQL. It
allows users to retrieve the specific
information they desire from the database.
INSERT
The INSERT command is used to add
records to an existing table.

DML Commands
UPDATE
The UPDATE command can be used to
modify information contained within a
table, either in bulk or individually.
DELETE
The DELETE command is used to delete
records from specified tables.

SQL Statements
Most of the actions you need to perform
on a database are done with SQL
Statements.
Every SQL command has its
corresponding SQL statement.
SQL is not case-sensitive; but for clarity,
SQL keywords will be written in UPPERCASE.

We will be using an imaginary database


called Northwind. A database that holds
customer data.

Customers Table
CustomerID

CustomerName ContactName

Address

City

PostalCode

Country

Alfreds
Futterkiste

Maria Anders

Obere Str.
57

Berlin

12209

Germany

Ana Trujillo
Emparedados
y helados

Ana Trujillo

Avda. de la Mxico D.F.


Constituci
n 2222

05021

Mexico

Antonio
Moreno
Taquera

Antonio
Moreno

Mataderos
2312

Mxico D.F.

05023

Mexico

Around the
Horn

Thomas Hardy

120
Hanover
Sq.

London

WA1 1DP

UK

Berglunds
snabbkp

Christina
Berglund

Berguvsvg Lule
en 8

S-958 22

Sweden

SQL SELECT
Statement

SELECT
The SELECT statement is used to select data from a
database. The result is stored in a result table, called
the result-set.
Syntax:
SELECT column_name1, column_name2,column_nameN
FROM table_name
-ORSELECT * FROM table_name

SELECT
The asterisk (*) is used here as a means
to select all columns of the specified table.

SELECT
Example
SELECT CustomerName, Country
FROM Customers
-columns: CustomerName and Country
-Table: Customers

SELECT
Result Set
CustomerName

Country

Alfreds Futterkiste

Germany

Ana Trujillo Emparedados y helados

Mexico

Antonio Moreno Taquera

Mexico

Around the Horn

UK

Berglunds snabbkp

Sweden

SELECT
Example
SELECT * FROM Customers
-columns: all columns
-Table: Customers

SELECT
Result Set
CustomerID CustomerNa ContactName Address
me

City

PostalCode

Country

Alfreds
Futterkiste

Berlin

12209

Germany

Ana Trujillo Ana Trujillo


Emparedado
s y helados

Avda. de Mxico D.F. 05021


la
Constituci
n 2222

Mexico

Antonio
Moreno
Taquera

Antonio
Moreno

Matadero Mxico D.F. 05023


s 2312

Mexico

Around the
Horn

Thomas
Hardy

120
Hanover
Sq.

WA1 1DP

UK

Berglunds
snabbkp

Christina
Berglund

Berguvsv Lule
gen 8

S-958 22

Sweden

Maria Anders Obere


Str. 57

London

SELECT DISTINCT
In a table, a column may contain many
duplicate values; and sometimes you only
want to list the different values.
SELECT DISTINCT statement is used to
return only distinct (different/unique)
values.

SELECT DISTINCT
Syntax:
SELECT DISTINCT
colum_name1,column_name2colum
n_nameN
FROM table_name

SELECT DISTINCT
Example:
SELECT DISTINCT City FROM
Customers

SELECT DISTINCT
Result - Set
City
Berlin
Mxico D.F.
London
Lule

Copies of Mexico D. F. were omitted.

SQL WHERE Clause


The WHERE clause is used to extract only
records that fulfill a specified criterion.
It is not limited only to selection but also is
applicable to any query that requires
specific record to handle.

SQL WHERE Clause


Syntax:
SELECT columns
FROM table_name
WHERE column_name operator value
Column_name after the WHERE word refers to
what column will the selection depends NOT the
columns to display.
Operator could be any arithmetic or logical
operator
Value will be the specified value in that column

SQL WHERE Clause


Example
SELECT * FROM Customers
WHERE Country = Mexico
The above statement will return the record of all
columns from the table Customers where the
value of Country column is Mexico

SQL WHERE Clause


Text Fields vs Numeric Fields
SQL requires single quotes around text
type/values. Numeric fields: fields that have
numeric datatype; should NOT be enclosed
in quotes.

SQL WHERE Clause


Result Set
CustomerID

CustomerName ContactName

Address

Ana Trujillo
Ana Trujillo
Emparedados y
helados

Antonio Moreno Antonio


Taquera
Moreno

City

PostalCode

Country

Avda. de Mxico D.F.


la
Constituc
in 2222

05021

Mexico

Matadero Mxico D.F.


s 2312

05023

Mexico

Operators in the WHERE


Clause
Operator

Description

Equal

<>

Not equal. Note: In some versions of SQL this operator


may be written as !=

>

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

SQL AND and OR Operators


The AND operator displays a record if both
the first and the second condition are true.
The OR operator displays a record if either
the first or the second condition is true
The AND and OR operators are used to
filter records based on more than one
condition

SQL AND and OR Operators


Example
SELECT * FROM Customers
WHERE Country = Germany
AND City = Berlin
The above query will return all records with all
columns from Customers table where its
country is Germany AND city is Berlin

SQL AND and OR Operators


Result Set
CustomerID

CustomerName ContactName

Address

Alfreds
Futterkiste

Obere Berlin
Str. 57

Maria
Anders

City

PostalCode

Country

12209

Germany

SQL AND and OR Operators


Example
SELECT * FROM Customers
WHERE City = Berlin
OR City = Mxico D.F.
The above query will return all records with all
columns from Customers table where its
country is Germany AND city is Berlin

SQL AND and OR Operators


Result Set
CustomerID CustomerNam ContactName Address City
e

PostalCode

Country

Alfreds
Futterkiste

Maria Anders Obere


Str. 57

12209

Germany

Ana Trujillo
Emparedados
y helados

Ana Trujillo

Avda.
Mxico D.F. 05021
de la
Constitu
cin
2222

Mexico

Antonio
Moreno
Taquera

Antonio
Moreno

Matader Mxico D.F. 05023


os 2312

Mexico

Berlin

SQL ORDER BY Keyword


The ORDER BY keyword is used to sort
the result-set by one or more columns.
The ORDER BY keyword sorts the
records in ascending order by default. To
sort the records in a descending order,
you can use the DESC keyword.

SQL ORDER BY Keyword


Syntax:
SELECT columns
FROM table_name
ORDER BY columns ASC/DESC

SQL ORDER BY Keyword


Example:
SELECT CustomerID, CustomerName
FROM Customers
ORDER BY City

SQL ORDER BY Keyword


Result Set
CustomerID

CustomerName

Alfreds Futterkiste

Around the Horn

Berglunds snabbkp

Ana Trujillo Emparedados


y helados

Antonio Moreno Taquera

The result-set above will display the columns of all


records arranged in ascending order based on its
city: Berlin, London, Lulea, Mexico D. F.

SQL ORDER BY Keyword


Example:
SELECT CustomerID, CustomerName
FROM Customers
ORDER BY City DESC

SQL ORDER BY Keyword


Result Set
CustomerID

CustomerName

Antonio Moreno Taquera

Ana Trujillo Emparedados


y helados

Berglunds snabbkp

Around the Horn

Alfreds Futterkiste

The result-set above will display the columns of all


records arranged in descending order based on its
city: Mexico D.F., Lulea, London, Berlin

SQL ORDER BY Keyword


Example:
SELECT CustomerID, CustomerName
FROM Customers
ORDER BY Country, ContactName

SQL ORDER BY Keyword


Result Set
CustomerID

CustomerName

Alfreds Futterkiste

Ana Trujillo Emparedados y


helados

Antonio Moreno Taquera

Berglunds snabbkp

Around the Horn

The result-set above will display the columns of all


records arranged in ascending order based 1st on its
country THEN if they have the same country, will then
sort by ContactName.

Vous aimerez peut-être aussi