4. SELECT Command Statement
• The SELECT statement is used to select data from a database
Examples:
The following SQL statement selects the "CustomerName" and "City" columns from
the "Customers" table:
SELECT CustomerName, City FROM Customers;
The following SQL statement selects all the columns from the "Customers" table:
SELECT * FROM Customers;
5. WHERE Command Statement
• The WHERE statement is used to filter records. The WHERE clause is used to extract
records that fulfil a specified criteria only.
Examples:
The following SQL statement selects all the customers from the country "Mexico", in the
"Customers" table:
SELECT * FROM Customers
WHERE Country='Mexico’;
However, numeric fields should not be enclosed in quotes:
SELECT * FROM Customers
WHERE CustomerID=1;
6. WHERE Clause Operators
Operator Description
= Equal
<> Not 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
7. AND Command Statement
• The AND and OR operators are used to filter records based on more than
one condition:
• The AND operator displays a record if all the conditions separated by AND is TRUE:
Example:
The following SQL statement selects all fields from "Customers" where
country is "Germany" AND city is "Berlin":
SELECT * FROM Customers
WHERE Country='Germany' AND City='Berlin';
8. OR Command Statement
• The AND and OR operators are used to filter records based on more than
one condition:
• The OR operator displays a record if any of the conditions separated by OR is TRUE.
Examples:
The following SQL statement selects all fields from "Customers" where city is
"Berlin" OR "München":
SELECT * FROM Customers
WHERE City='Berlin' OR City='München';
9. NOT Command Statement
• The NOT operator displays a record if the condition(s) is NOT TRUE:
Examples:
The following SQL statement selects all fields from "Customers" where
country is NOT "Germany":
SELECT * FROM Customers
WHERE NOT Country='Germany';
10. Combining AND, OR and NOT operators
• You can also combine the AND, OR and NOT operators:
Examples:
The following SQL statement selects all fields from "Customers" where country is
"Germany" AND city must be "Berlin" OR "München":
SELECT * FROM Customers
WHERE Country='Germany' AND (City='Berlin' OR City='München’);
The following SQL statement selects all fields from "Customers" where country is
NOT "Germany" and NOT "USA":
SELECT * FROM Customers
WHERE NOT Country='Germany' AND NOT Country='USA';
11. INSERT INTO Command Statement
• The INSERT INTO statement is used to insert new records in a table:
Syntax:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Examples:
The following SQL statement inserts a new record in the "Customers" table:
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode,
Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');
12. INSERT INTO only specified table columns
• It is also possible to only insert data in specific columns.
Examples:
The following SQL statement will insert a new record, but only insert
data in the "CustomerName", "City", and "Country" columns:
INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway’);
13. SQL NULL Values
• A field with a NULL value is a field with no value.
Note:
A NULL value is different from a zero value or a field that contains
spaces. A field with a NULL value is one that has been left blank during
record creation.
14. UPDATE Command Statement
• The UPDATE statement is used to modify the existing records in a table.
Example:
The following SQL statement updates the first customer (CustomerID = 1)
with a new contact person and a new city (Alfred and Frankfurt are new
values):
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
15. UPDATE Multiple Records
• It is the WHERE clause that determines how many records that will be
updated:
Example:
The following SQL statement will update the contactname to "Juan" for all
records where country is "Mexico":
UPDATE Customers
SET ContactName='Juan'
WHERE Country='Mexico’;
Note: Missing out the WHERE statement will alter every value under SET
16. DELETE Command Statement
• The DELETE statement is used to delete existing records in a table.
Example:
The following SQL statement deletes the customer “John Smith" from the
"Customers" table:
DELETE FROM Customers
WHERE CustomerName=‘John Smith’;
Note: To delete all from a table, use:
DELETE * FROM table_name;
17. CREATE DATABASE Command Statement
• The CREATE DATABASE statement is used to create a new SQL
database:
Syntax:
CREATE DATABASE databasename;
Example:
CREATE DATABASE testDB;
18. DROP DATABASE Command Statement
• The DROP DATABASE statement is used to drop an existing SQL database:
Syntax:
DROP DATABASE databasename;
Example:
DROP DATABASE testDB;
19. CREATE TABLE Command Statement
• The CREATE TABLE statement is used to create a new table in a
database:
Syntax:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
21. DROP TABLE Command Statement
• The DROP TABLE statement is used to drop an existing table in a
database.
Syntax:
DROP TABLE table_name;
Example:
DROP TABLE testtable;
22. TRUNCATE TABLE Command Statement
• The TRUNCATE TABLE statement is used to delete the data inside a
table, but not the table itself.
Syntax:
TRUNCATE TABLE table_name;
Example:
TRUNCATE TABLE testtable;
23. ALTER TABLE Command Statement
• The ALTER TABLE statement is used to add, delete, or modify columns
in an existing table.
• The ALTER TABLE statement is also used to add and drop various
constraints on an existing table.
Syntax:
To add a column in a table, use the following syntax:
ALTER TABLE table_name
ADD column_name datatype;
24. ALTER TABLE continued…
Syntax:
To delete a column in a table, use the following syntax:
ALTER TABLE table_name
DROP COLUMN column_name;
Examples:
ALTER TABLE testtable
ADD DateOfBirth date;
25. ALTER TABLE continued…
Examples:
The following deletes the column named "DateOfBirth" in the “testtable"
table:
ALTER TABLE Persons
DROP COLUMN DateOfBirth;
The following changes the data type of the column named "DateOfBirth" in
the “testtable" table (date to year):
ALTER TABLE Persons
ALTER COLUMN DateOfBirth year;
26. SQL Constraints Examples
• SQL constraints are used to specify rules for the data in a table:
• The following constraints are commonly used in SQL:
• NOT NULL - Ensures that a column cannot have a NULL value
• UNIQUE - Ensures that all values in a column are different
• PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely
identifies each row in a table
• FOREIGN KEY - Uniquely identifies a row/record in another table
• CHECK - Ensures that all values in a column satisfies a specific condition
• DEFAULT - Sets a default value for a column when no value is specified
• INDEX - Used to create and retrieve data from the database very quickly
27. PRIMARY KEY Constraint
• The PRIMARY KEY constraint uniquely identifies each record in a database
table.
• Primary keys must contain UNIQUE values, and cannot contain NULL
values.
PRIMARY KEY on CREATE TABLE:
CREATE TABLE testtable (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
28. PRIMARY KEY continued…
PRIMARY KEY on ALTER TABLE:
ALTER TABLE testtable
ADD PRIMARY KEY (ID);
DROP a PRIMARY KEY:
ALTER TABLE testtable
DROP PRIMARY KEY;
29. FOREIGN KEY Constraint
• A FOREIGN KEY is a key used to link two tables together.
• A FOREIGN KEY is a field (or multiple) in one table that refers to the
PRIMARY KEY in another table:
FOREIGN KEY on CREATE TABLE:
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES testtable(PersonID)
);
30. FOREIGN KEY continued…
FOREIGN KEY on ALTER TABLE:
ALTER TABLE Orders
ADD FOREIGN KEY (PersonID) REFERENCES testtable(PersonID);
DROP a FOREIGN KEY:
ALTER TABLE Orders
DROP FOREIGN KEY FK_PersonOrder;