Introduction To SQL: Prepared By: Romer Ian O. Pasoc
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
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
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.
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
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
Mexico
Mexico
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
Mexico
Antonio
Moreno
Taquera
Antonio
Moreno
Mexico
Around the
Horn
Thomas
Hardy
120
Hanover
Sq.
WA1 1DP
UK
Berglunds
snabbkp
Christina
Berglund
Berguvsv Lule
gen 8
S-958 22
Sweden
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
CustomerName ContactName
Address
Ana Trujillo
Ana Trujillo
Emparedados y
helados
City
PostalCode
Country
05021
Mexico
05023
Mexico
Description
Equal
<>
>
Greater than
<
Less than
>=
<=
BETWEEN
LIKE
IN
CustomerName ContactName
Address
Alfreds
Futterkiste
Obere Berlin
Str. 57
Maria
Anders
City
PostalCode
Country
12209
Germany
PostalCode
Country
Alfreds
Futterkiste
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
Mexico
Berlin
CustomerName
Alfreds Futterkiste
Berglunds snabbkp
CustomerName
Berglunds snabbkp
Alfreds Futterkiste
CustomerName
Alfreds Futterkiste
Berglunds snabbkp