Structured
Query
Language
[SQL]
What is SQL?
○ SQL stands for Structured Query Language
○ SQL lets you access and manipulate databases
○ SQL became a standard of the ANSI in 1986, and
of the ISO in 1987
○ It is very popular language used to manipulate
the data bases.
2
What Can SQL do?
○ SQL can retrieve data from a database
○ SQL can insert, update, delete records in a
database
○ SQL can create, delete new databases and
Tables
○ SQL can create stored procedures in a database
○ SQL can create views in a database
○ SQL can set permissions on tables, procedures,
and views 3
SQL is a Standard.
○ Although SQL is an ANSI/ISO standard, there are
different versions of the SQL language.
○ However,
○ In order to be compliant with the ANSI standard,
they all support at least the major commands
○ Such as SELECT, UPDATE, DELETE, INSERT, WHERE in a
similar manner.
4
RDBMS & SQL
○ RDBMS stands for Relational Database
Management System.
○ RDBMS is the basis for SQL, and for all modern
database systems such as MS SQL Server, IBM
DB2, Oracle, MySQL, and Microsoft Access.
5
Database Tables
6
SQL Statements
○ Using SQL statements you can do many things in
a data base.
○ The following SQL statement selects all the
records in the "Contacts" table that we saw
earlier.
○ SELECT * FROM Contacts;
7
Things to Remember
○ SQL keywords are NOT case sensitive:
select is the same as SELECT
○ Some database systems require a semicolon at
the end of each SQL statement. But in access, you
can write SQL commands without semi colon.
8
Frequently used SQL Commands
○ SELECT - extracts data from a database
○ UPDATE - updates data in a database
○ DELETE - deletes data from a database
○ INSERT INTO - inserts new data into a database
○ CREATE DATABASE - creates a new database
○ ALTER DATABASE - modifies a database 9
Frequently used SQL Commands
○ CREATE TABLE - creates a new table
○ ALTER TABLE - modifies a table
○ DROP TABLE - deletes a table
○ CREATE INDEX - creates an index (search key)
○ DROP INDEX - deletes an index
10
SQL SELECT Statement
○ The WHERE clause is used to filter records.
○ It is used to extract only those records that fulfill a
specified condition.
11
SELECT column1, column2, ...
FROM table_name;
OR
SELECT * FROM table_name;
12
SELECT Column Example (10 Mins)
○ Write SQL statement to select the “Index_No" and
“Full_Name" columns from the "Contacts" table.
SELECT Index_No,Full_Name FROM Contacts;
13
14
SELECT All Columns(05 Mins)
○ Write SQL statement to select the all columns
from the "Contacts" table.
SELECT * FROM Contacts;
15
SQL WHERE Clause
○ Write SQL statement to select the “Index_No" and
“Full_Name" columns from the "Contacts" table.
SELECT Index_No,Full_Name FROM Contacts;
16
SELECT column1, column2, ...
FROM table_name
WHERE condition;
17
Where Clause (10 Mins)
○ Write SQL statement to select the “Index_No" and
“Full_Name" columns of male students from the
"Contacts" table.
SELECT Index_No, Full_Name FROM Contacts
WHERE Gender = “M”;
18
Text vs Numeric
○ SQL requires single quotes around text values
(most database systems will also allow double
quotes).
○ However, numeric fields should not be enclosed in
quotes:
19
Where Clause Number (05 Mins)
○ Write SQL statement to select the “Index_No" and
“Full_Name" columns of whose grade is 12 from
the "Contacts" table.
SELECT Index_No, Full_Name FROM Contacts
WHERE Grade = 12;
20
Operators in The WHERE Clause
= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
21
Operators in The WHERE Clause
<> Not equal. Note: In some versions of SQL
this operator may be written as !=
BETWEEN Between a certain range
LIKE Search for a pattern
IN To specify multiple possible values for a
column
22
Where Clause Number (10 Mins)
○ Write SQL statement to select the “Index_No" and
“Full_Name" columns of whose grade between 8
and 12 from the "Contacts" table.
SELECT Index_No, Full_Name FROM Contacts
WHERE Grade BETWEEN 12 AND 8;
23
The SQL INSERT INTO Statement
○ The INSERT INTO statement is used to insert new
records in a table.
○ INSERT INTO statement can be used in two ways.
○ 01. Specify both the column names and the values to be inserted:
○ 02. Adding values for all the columns of the table, without
specifying the column names.
24
1st Method
INSERT INTO table_name (column1, column2,
column3, ...)
VALUES (value1, value2, value3, ...);
25
2nd Method
INSERT INTO table_name
VALUES(value1, value2, value3, ...);
26
INSERTING Data (10 Minutes)
○ Write SQL statement to Enter data to Subjects
table.
INSERT INTO Subjects
VALUES(“Access”, “ICT121”, 1, 1,001);
27
28
INSERTING Data to Specific
Columns (10 Minutes)
○ Write SQL statement to Enter data to Sub_code
and Teacher_ID in subjects table.
INSERT INTO Subjects(Sub_Code,Teacher_ID)
VALUES “ICT121”,001);
29
SQL Null Values
○ A field with a NULL value is a field with no value.
○ If a field in a table is optional, it is possible to
insert a new record or update a record without
adding a value to this field. Then, the field will be
saved with a NULL value.
30
How to Find Null Values
○ It is not possible to test for NULL values with
comparison operators, such as =, <, or <>.
○ We will have to use the IS NULL and IS NOT NULL
operators instead.
31
Finding Data with NULL Values
(10 Minutes)
○ Write SQL statement to Find Subject_ID and
Sub_Code where Sub_Name is null in subjects
table.
SELECT Subject_ID, Sub_Code FROM Subjects
WHERE Sub_Name IS NULL;
32
33
Finding Data with NULL Values
(10 Minutes)
○ Write SQL statement to Find Subject_ID and
Sub_Code where Sub_Name is null in subjects
table.
SELECT Subject_ID, Sub_Code FROM Subjects
WHERE Sub_Name IS NULL;
34
The SQL UPDATE Statement
○ The UPDATE statement is used to modify the
existing records in a table.
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
35
Updating Data (10 Minutes)
○ Write SQL statement to update Subject_ID = 03
Sub_Name and Sub_Code values in subjects table.
UPDATE Subjects
SET Sub_Name = “English”,Sub_Code = “ENG111”
WHERE Subject_ID = 3;
36
SQL UPDATE – Be Careful
(10 Minutes)
○ Be careful when updating records. If you omit the
WHERE clause, ALL records will be updated!
Make a copy of subject table with structure and
data and try this.
UPDATE Subjects
SET Sub_Name =“English”; 37
The SQL DELETE Statement
○ The DELETE statement is used to delete existing
records in a table.
DELETE FROM table_name
WHERE condition;
38
Deleting Data (10 Minutes)
○ Write SQL statement to delete Subject_ID = 03
Sub_Name and Sub_Code values in subjects
table.
DELETE FROM Subjects
WHERE Subject_ID = 2;
39
Deleting All Data without table
(10 Minutes)
It is possible to delete all rows in a table without
deleting the table. This means that the table
structure, attributes, and indexes will be intact:
DELETE FROM Subjects;
40
Cont’d
41