0% found this document useful (0 votes)
37 views

CH 3 SQL

SQL is a standard language for accessing and manipulating databases. It can perform functions like querying data, inserting records, updating records, and deleting records. SQL commands are divided into categories like data query language, data manipulation language, and data definition language. Some common SQL commands include SELECT, INSERT, UPDATE, DELETE, CREATE TABLE, and ALTER TABLE.

Uploaded by

Robel Haftom
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

CH 3 SQL

SQL is a standard language for accessing and manipulating databases. It can perform functions like querying data, inserting records, updating records, and deleting records. SQL commands are divided into categories like data query language, data manipulation language, and data definition language. Some common SQL commands include SELECT, INSERT, UPDATE, DELETE, CREATE TABLE, and ALTER TABLE.

Uploaded by

Robel Haftom
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

Chapter 3

SQL

1 06/07/2023
What is SQL?

 SQL stands for Structured Query Language.


 SQL is a standard language for accessing
databases.
 SQL is an ANSI (American National Standards
Institute) standard

2 06/07/2023
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
 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 06/07/2023
RDBMS
 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.
 The data in RDBMS is stored in database objects called
tables.
 A table is a collection of related data entries and it consists of
columns and rows.

4 06/07/2023
SQL Commands

 The standard SQL commands to interact with


relational databases are CREATE, SELECT,
INSERT, UPDATE, DELETE, and DROP. These
commands can be classified into groups based on
their nature:

5 06/07/2023
Data Definition Language (DDL)

Data Definition Language (DDL) statements are used to define the


database structure or schema. Some examples:
 CREATE - to create objects in the database
 ALTER - alters the structure of the database
 DROP - delete objects from the database
 TRUNCATE - remove all records from a table, including all
spaces allocated for the records are removed
 COMMENT - add comments to the data dictionary
 RENAME - rename an object

6 06/07/2023
Data Manipulation Language (DML)

Data Manipulation Language (DML) statements are used for managing


data within schema objects. Some examples:
 SELECT - retrieve data from the a database
 INSERT - insert data into a table
 UPDATE - updates existing data within a table
 DELETE - deletes all records from a table, the space for the records
remain
 MERGE - UPSERT operation (insert or update)
 CALL - call a PL/SQL or Java subprogram
 EXPLAIN PLAN - explain access path to data
 LOCK TABLE - control concurrency

7 06/07/2023
Data Control Language  (DCL)

Data Control Language (DCL) statements. examples:


 GRANT - gives user's access privileges to database
 REVOKE - withdraw access privileges given with
the GRANT command

8 06/07/2023
SQL Data Types for Various DBs

Microsoft Access Data Types

 Text  AutoNumber
 Memo  Date/Time
 Integer  Yes/No
 Single  Ole Object
 Double  Lookup Wizard
 Currency  Hyperlink
 Byte

9 06/07/2023
SQL Syntax

Database Tables
 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.
 Sample database (included in MS Access and MS
SQL Server).
 Below is a selection from the "Customers" table:

10 06/07/2023
Customers table
Customer Customer Contact Address city Postal Country
ID Name Name Code

1 Alfred's Maria Obere Str. Berlin 12209 Germany


57

2 Ana Ana Trujillo Avda. de la México 05021 Mexico


Trujillo

3 Antonio Antonio Matadors México 05023 Mexico


Moreno 2312
4
Around Thomas 120 London WA1 UK
the Hardy Hanover 1DP
Sq.

5 Berglund Christina Berguvsväg Luleå S-958 Sweden


ss en 8 22

11 06/07/2023
cont’d…

 The table above contains five records (one for each


customer) and seven columns (CustomerID,
Customer Name, Contact Name, Address, City, Postal
Code, and Country).

12 06/07/2023
SQL Statements

Most of the actions you need to perform on a database are


done with SQL statements.
 The following SQL statement selects all the records in the
"Customers" table:
• Example:
 SELECT * FROM Customers;
Keep in Mind That...
 SQL is NOT case sensitive: SELECT is the same as select

13 06/07/2023
Semicolon after SQL Statements?

 Semicolon after SQL Statements?


 Some database systems require a semicolon at the end of
each SQL statement.
 Semicolon is the standard way to separate each SQL
statement in database systems that allow more than one
SQL statement to be executed in the same call to the
server.
 Due to this we will use semicolon at the end of each SQL
statement.
14 06/07/2023
Some of The Most Important SQL Commands

 SELECT :- extracts data from a  CREATE TABLE  :- creates a


database
new table
 UPDATE  :- updates data in a
database
 ALTER TABLE  :- modifies a
 DELETE  :- deletes data from a table
database
 DROP TABLE  :- deletes a table
 INSERT INTO  :- inserts new data
into a database
 CREATE INDEX  :- creates an
 CREATE DATABASE  :- creates a index (search key)
new database
 DROP INDEX  :- deletes an
 ALTER DATABASE  :- modifies a
index
database

15 06/07/2023
SQL SELECT Statement

 The SELECT statement is used to select data from a database.


The result is stored in a result table, called the result-set.

 SQL SELECT Syntax


SELECT column_name,column_name
FROM table_name;

and

SELECT * FROM table_name;

16 06/07/2023
SELECT Column Example

 The following SQL statement selects the "Customer


Name" and "City" columns from the "Customers"
table:

Example:

SELECT CustomerName,City FROM Customers;

17 06/07/2023
SELECT * Example

 The following SQL statement selects all the columns


from the "Customers" table:

Example:

SELECT * FROM Customers;

18 06/07/2023
SQL SELECT DISTINCT statement

 The SELECT DISTINCT statement is used to return


only distinct (different) values.
 The SQL SELECT DISTINCT Statement
 In a table, a column may contain many duplicate values
and sometimes you only want to list the different (distinct)
values.

 The DISTINCT keyword can be used to return only


distinct (different) values.

19 06/07/2023
SQL SELECT DISTINCT Syntax

 SELECT DISTINCT column_name,column_name
FROM table_name;

20 06/07/2023
SQL WHERE Clause

 The WHERE clause is used to filter records.


 The WHERE clause is used to extract only those records that
fulfill a specified criterion.
 SQL WHERE Syntax
 SELECT column_name,column_name
FROM table_name
WHERE column name operator value;

21 06/07/2023
WHERE Clause Example

 The following SQL statement selects all the


customers from the country "Mexico", in the
"Customers" table:

SELECT * FROM Customers


WHERE Country='Mexico';

22 06/07/2023
SQL AND & OR Operators

 The AND & OR operators are used to filter records based on


more than one condition.
 The AND operator displays a record if both the first
condition AND the second condition are true.
 The OR operator displays a record if either the first
condition OR the second condition is true.

23 06/07/2023
AND Operator Example

 The following SQL statement selects all customers


from the country "Germany" AND the city "Berlin",
in the "Customers" table:

Example:

SELECT * FROM Customers


WHERE Country='Germany'
AND City='Berlin';

24 06/07/2023
OR Operator Example

 The following SQL statement selects all customers


from the city "Berlin" OR "München", in the
"Customers" table: 

Example:

SELECT * FROM Customers


WHERE City='Berlin'
OR City='München';

25 06/07/2023
Combining AND & OR

 You can also combine AND OR (use parenthesis to form

complex expressions).
 The following SQL statement selects all customers from the
country "Germany" AND the city must be equal to "Berlin" OR
"München", in the "Customers" table:

Example:

SELECT * FROM Customers


WHERE Country='Germany'
AND (City='Berlin' OR City='München');

26 06/07/2023
SQL CREATE TABLE Statement

 The CREATE TABLE statement is used to create a


table in a database.
 Tables are organized into rows and columns; and
each table must have a name.

27 06/07/2023
Cont’d…

 SQL CREATE TABLE Syntax

CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);

28 06/07/2023
Cont’d…

 The column_name parameters specify the names of


the columns of the table.
 The data_type parameter specifies what type of data
the column can hold (e.g. varchar, integer, decimal,
date, etc.).
 The size parameter specifies the maximum length of
the column of the table.

29 06/07/2023
SQL CREATE TABLE Example

 Now we want to create a table called "Persons" that contains five


columns: PersonID, Last Name, First Name, Address, and City.
 Therefore, We use the following CREATE TABLE statement:

CREATE TABLE Persons


(
PersonID int,
Last Name varchar(255),
First Name varchar(255),
Address varchar(255),
City varchar(255)
);
30 06/07/2023
SQL INSERT INTO Statement

 The INSERT INTO statement is used to insert new records in a table.


 SQL INSERT INTO Syntax
 It is possible to write the INSERT INTO statement in two forms.
 The first form does not specify the column names where the data will be
inserted, only their values:
 INSERT INTO table_name
VALUES (value1,value2,value3,...);
 The second form specifies both the column names and the values to be inserted:
 INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);

31 06/07/2023
cont’d…

 Assume we wish to insert a new row in the "Customers" table.


 We can use the following SQL statement (without specifying column
names):
 INSERT INTO Customers
VALUES ('Cardinal','Tom B. Erichsen','Skagen
21','Stavanger','4006','Norway');

or this SQL statement (including column names):


 INSERT INTO Customers (CustomerName, ContactName, Address, City,
PostalCode, Country)
VALUES ('Cardinal','Tom B. Erichsen','Skagen
21','Stavanger','4006','Norway');
32 06/07/2023
Insert Data Only in Specified Columns

 It is also possible to only insert data in specific columns.


 The following SQL statement will insert a new row, but only
insert data in the "CustomerName", "City", and "Country"
columns (and the CustomerID field will of course also be
updated automatically):

INSERT INTO Customers (CustomerName, City, Country)


VALUES ('Cardinal', 'Stavanger', 'Norway');

33 06/07/2023
The SQL UPDATE Statement

 The UPDATE statement is used to update existing


records in a table.
 SQL UPDATE Syntax

UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;

34 06/07/2023
SQL UPDATE Example

 Assume we wish to update the customer "Alfreds


Futterkiste" with a new contact person and city.
 We use the following SQL statement:

UPDATE Customers
SET ContactName='Alfred Schmidt', City='Hamburg'
WHERE CustomerName='Alfreds Futterkiste';

35 06/07/2023
SQL DELETE Statement

 The DELETE statement is used to delete rows in a


table.
 SQL DELETE Syntax

DELETE FROM table_name
WHERE some_column=some_value;

36 06/07/2023
SQL DELETE Example

 Assume we wish to delete the customer "Alfreds


Futterkiste" from the "Customers" table.
 We use the following SQL statement:

DELETE FROM Customers


WHERE CustomerName='Alfreds Futterkiste' AND
ContactName='Maria Anders';

37 06/07/2023
Delete All Data

 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 table_name;
or
DELETE * FROM table_name;

38 06/07/2023
SQL LIKE Operator

 The LIKE operator is used to search for a specified


pattern in a column.
 SQL LIKE Syntax

SELECT column name(s)
FROM table_name
WHERE column name LIKE pattern;

39 06/07/2023
SQL LIKE Operator Examples

 The following SQL statement selects all customers


with a City starting with the letter "s":

Example

SELECT * FROM Customers


WHERE City LIKE 's%';

40 06/07/2023
cont’d…

 The following SQL statement selects all customers


with a City ending with the letter "s":

Example

SELECT * FROM Customers


WHERE City LIKE '%s';

41 06/07/2023
The SQL BETWEEN Operator

 The BETWEEN operator selects values within a range. The


values can be numbers, text, or dates.
 SQL BETWEEN Syntax

SELECT column name(s)
FROM table_name
WHERE column name BETWEEN value1 AND value2;

42 06/07/2023
BETWEEN Operator Example

 The following SQL statement selects all products


with a price BETWEEN 10 and 20:

Example

SELECT * FROM Products


WHERE Price BETWEEN 10 AND 20;

43 06/07/2023
BETWEEN Operator with Text Value

 The following SQL statement selects all products


with a Product Name beginning with any of the
letter BETWEEN 'C' and 'M':

Example

SELECT * FROM Products


WHERE Product Name BETWEEN 'C' AND 'M';

44 06/07/2023

You might also like