Chapter 7:
SQL, the Structured
Query Language
Soid Quintero & Ervi Bongso
CS157B
Overview
Introduction
DDL Commands
DML Commands
SQL Statements, Operators, Clauses
Aggregate Functions
The ANSI standard language for the definition
and manipulation of relational database.
Includes data definition language (DDL),
statements that specify and modify database
schemas.
Includes a data manipulation language (DML),
statements that manipulate database content.
Structured Query Language (SQL)
Some Facts on SQL
SQL data is case-sensitive, SQL commands are not.
First Version was developed at IBM by Donald D.
Chamberlin and Raymond F. Boyce. [SQL]
Developed using Dr. E.F. Codd's paper, “A Relational
Model of Data for Large Shared Data Banks.”
SQL query includes references to tuples variables
and the attributes of those variables
SQL: DDL Commands
CREATE TABLE: used to create a table.
ALTER TABLE: modifies a table after it was created.
DROP TABLE: removes a table from a database.
SQL: CREATE TABLE Statement
Things to consider before you create your table are:
The type of data
the table name
what column(s) will make up the primary key
the names of the columns
CREATE TABLE statement syntax:
CREATE TABLE <table name>
( field1 datatype ( NOT NULL ),
field2 datatype ( NOT NULL )
);
SQL: Attributes Types
Table 7.6 pg.164
SQL: ALTER TABLE Statement
To add or drop columns on existing tables.
ALTER TABLE statement syntax:
ALTER TABLE <table name>
ADD attr datatype;
or
DROP COLUMN attr;
SQL: DROP TABLE Statement
Has two options:
CASCADE: Specifies that any foreign key constraint
violations that are caused by dropping the table will
cause the corresponding rows of the related table to
be deleted.
RESTRICT: blocks the deletion of the table of any
foreign key constraint violations would be created.
DROP TABLE statement syntax:
DROP TABLE <table name> [ RESTRICT|CASCADE ];
Example:
CREATE TABLE FoodCart
(
date varchar(10),
food varchar(20),
profit float
);
ALTER TABLE FoodCart (
ADD sold int
);
ALTER TABLE FoodCart(
DROP COLUMN profit
);
DROP TABLE FoodCart;
profit
food
date
sold
profit
food
date
sold
food
date
FoodCart
FoodCart
FoodCart
SQL: DML Commands
INSERT: adds new rows to a table.
UPDATE: modifies one or more attributes.
DELETE: deletes one or more rows from a table.
SQL: INSERT Statement
To insert a row into a table, it is necessary to
have a value for each attribute, and order
matters.
INSERT statement syntax:
INSERT into <table name>
VALUES ('value1', 'value2', NULL);
Example: INSERT into FoodCart
VALUES (’02/26/08', ‘pizza', 70 );
FoodCart
70
pizza
02/26/08
500
hotdog
02/26/08
350
pizza
02/25/08
sold
food
date
500
hotdog
02/26/08
350
pizza
02/25/08
sold
food
date
SQL: UPDATE Statement
To update the content of the table:
UPDATE statement syntax:
UPDATE <table name> SET <attr> = <value>
WHERE <selection condition>;
Example: UPDATE FoodCart SET sold = 349
WHERE date = ’02/25/08’ AND food = ‘pizza’;
FoodCart
70
pizza
02/26/08
500
hotdog
02/26/08
350
pizza
02/25/08
sold
food
date
70
pizza
02/26/08
500
hotdog
02/26/08
349
pizza
02/25/08
sold
food
date
SQL: DELETE Statement
To delete rows from the table:
DELETE statement syntax:
DELETE FROM <table name>
WHERE <condition>;
Example: DELETE FROM FoodCart
WHERE food = ‘hotdog’;
FoodCart
Note: If the WHERE clause is omitted all rows of data are deleted from the table.
70
pizza
02/26/08
500
hotdog
02/26/08
349
pizza
02/25/08
sold
food
date
70
pizza
02/26/08
349
pizza
02/25/08
sold
food
date
SQL Statements, Operations, Clauses
SQL Statements:
Select
SQL Operations:
Join
Left Join
Right Join
Like
SQL Clauses:
Order By
Group By
Having
SQL: SELECT Statement
A basic SELECT statement includes 3 clauses
SELECT <attribute name> FROM <tables> WHERE <condition>
SELECT
Specifies the
attributes that are
part of the
resulting relation
FROM
Specifies the
tables that serve
as the input to the
statement
WHERE
Specifies the
selection condition,
including the join
condition.
Note: that you don't need to use WHERE
Using a “*” in a select statement indicates that
every attribute of the input table is to be
selected.
Example: SELECT * FROM … WHERE …;
To get unique rows, type the keyword
DISTINCT after SELECT.
Example: SELECT DISTINCT * FROM …
WHERE …;
SQL: SELECT Statement (cont.)
Example:
Person
80
34
Peter
54
54
Helena
70
29
George
64
28
Sally
80
34
Harry
Weight
Age
Name
80
34
Peter
54
54
Helena
80
34
Harry
Weight
Age
Name
80
54
80
Weight
1) SELECT *
FROM person
WHERE age > 30;
2) SELECT weight
FROM person
WHERE age > 30;
3) SELECT distinct weight
FROM person
WHERE age > 30;
54
80
Weight
SQL: Join operation
A join can be specified in the FROM clause
which list the two input relations and the
WHERE clause which lists the join
condition.
Example:
Biotech
1003
Sales
1002
IT
1001
Division
ID
TN
1002
MA
1001
CA
1000
State
ID
Emp Dept
SQL: Join operation (cont.)
Sales
1002
IT
1001
Dept.Division
Dept.ID
TN
1002
MA
1001
Emp.State
Emp.ID
inner join = join
SELECT *
FROM emp join dept (or FROM emp, dept)
on emp.id = dept.id;
SQL: Join operation (cont.)
IT
1001
Sales
1002
null
null
Dept.Division
Dept.ID
CA
1000
TN
1002
MA
1001
Emp.State
Emp.ID
left outer join = left join
SELECT *
FROM emp left join dept
on emp.id = dept.id;
SQL: Join operation (cont.)
Sales
1002
Biotech
1003
IT
1001
Dept.Division
Dept.ID
MA
1001
null
null
TN
1002
Emp.State
Emp.ID
right outer join = right join
SELECT *
FROM emp right join dept
on emp.id = dept.id;
SQL: Like operation
Pattern matching selection
% (arbitrary string)
SELECT *
FROM emp
WHERE ID like ‘%01’;
 finds ID that ends with 01, e.g. 1001, 2001, etc
_ (a single character)
SELECT *
FROM emp
WHERE ID like ‘_01_’;
 finds ID that has the second and third character
as 01, e.g. 1010, 1011, 1012, 1013, etc
SQL: The ORDER BY Clause
Ordered result selection
desc (descending order)
SELECT *
FROM emp
order by state desc
 puts state in descending order, e.g. TN, MA, CA
asc (ascending order)
SELECT *
FROM emp
order by id asc
 puts ID in ascending order, e.g. 1001, 1002, 1003
SQL: The GROUP BY Clause
The function to divide the tuples into groups and
returns an aggregate for each group.
Usually, it is an aggregate function’s companion
SELECT food, sum(sold) as totalSold
FROM FoodCart
group by food;
FoodCart
419
pizza
500
hotdog
totalSold
food
70
pizza
02/26/08
500
hotdog
02/26/08
349
pizza
02/25/08
sold
food
date
SQL: The HAVING Clause
The substitute of WHERE for aggregate functions
Usually, it is an aggregate function’s companion
SELECT food, sum(sold) as totalSold
FROM FoodCart
group by food
having sum(sold) > 450;
FoodCart
500
hotdog
totalSold
food
70
pizza
02/26/08
500
hotdog
02/26/08
349
pizza
02/25/08
sold
food
date
SQL: Aggregate Functions
Are used to provide summarization information for
SQL statements, which return a single value.
COUNT(attr)
SUM(attr)
MAX(attr)
MIN(attr)
AVG(attr)
Note: when using aggregate functions, NULL values
are not considered, except in COUNT(*) .
SQL: Aggregate Functions (cont.)
COUNT(attr) -> return # of rows that are not null
Ex: COUNT(distinct food) from FoodCart; -> 2
SUM(attr) -> return the sum of values in the attr
Ex: SUM(sold) from FoodCart; -> 919
MAX(attr) -> return the highest value from the attr
Ex: MAX(sold) from FoodCart; -> 500
70
pizza
02/26/08
500
hotdog
02/26/08
349
pizza
02/25/08
sold
food
date
FoodCart
SQL: Aggregate Functions (cont.)
MIN(attr) -> return the lowest value from the attr
Ex: MIN(sold) from FoodCart; -> 70
AVG(attr) -> return the average value from the attr
Ex: AVG(sold) from FoodCart; -> 306.33
Note: value is rounded to the precision of the datatype
70
pizza
02/26/08
500
hotdog
02/26/08
349
pizza
02/25/08
sold
food
date
FoodCart
Riccardi, Greg. Principles of Database Systems with Internet and Java Applications.
Addison Wesley, 2001.
Ronald R. Plew, Ryan K. Stephens. Teach Yourself SQL in 24 Hours 3rd Edition.
Sams Publishing, 2003.
SQL http://en.wikipedia.org/wiki/SQL
W3C http://www.w3schools.com/sql/sql_tryit.asp
Wikipedia - SQL http://en.wikipedia.org/wiki/SQL
Wikipedia - join http://en.wikipedia.org/wiki/Join_(SQL)
References

More Related Content

PPT
Sql basic best-course-in-mumbai
PPT
SQL.ppt
PPT
Sql presentation 1 by chandan
DOCX
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
PPTX
Creating database using sql commands
PPTX
SQL - DML and DDL Commands
PPTX
SQL Query
PPT
chapter 8 SQL.ppt
Sql basic best-course-in-mumbai
SQL.ppt
Sql presentation 1 by chandan
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Creating database using sql commands
SQL - DML and DDL Commands
SQL Query
chapter 8 SQL.ppt

Similar to SQL Presentation-1 yehjebjj yeuu helo the worls.ppt (20)

PDF
Database Architecture and Basic Concepts
PPTX
Introduction to sql new
PPT
PPTX
Introduction to database and sql fir beginers
ODP
PPTX
DOCX
PDF
Creating, altering and dropping tables
PDF
SQL-Notes.pdf mba students database note
PDF
PPT
PPTX
STRUCTURE OF SQL QUERIES
PPT
chap 7.ppt(sql).ppt
PPT
Select To Order By
PPT
Java Database Connectivity (JDBC) with Spring Framework is a powerful combina...
PPT
Ms sql server ii
DOC
Oracle sql material
PPT
Lab_04.ppt opreating system of computer lab
PPT
Sql 2006
Database Architecture and Basic Concepts
Introduction to sql new
Introduction to database and sql fir beginers
Creating, altering and dropping tables
SQL-Notes.pdf mba students database note
STRUCTURE OF SQL QUERIES
chap 7.ppt(sql).ppt
Select To Order By
Java Database Connectivity (JDBC) with Spring Framework is a powerful combina...
Ms sql server ii
Oracle sql material
Lab_04.ppt opreating system of computer lab
Sql 2006
Ad

Recently uploaded (20)

DOCX
Modern SharePoint Intranet Templates That Boost Employee Engagement in 2025.docx
PPTX
Tech Workshop Escape Room Tech Workshop
PPTX
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
PPTX
Oracle Fusion HCM Cloud Demo for Beginners
PDF
EaseUS PDF Editor Pro 6.2.0.2 Crack with License Key 2025
PDF
How Tridens DevSecOps Ensures Compliance, Security, and Agility
PPTX
assetexplorer- product-overview - presentation
PDF
Ableton Live Suite for MacOS Crack Full Download (Latest 2025)
PPTX
Weekly report ppt - harsh dattuprasad patel.pptx
PDF
The Dynamic Duo Transforming Financial Accounting Systems Through Modern Expe...
PPTX
Cybersecurity: Protecting the Digital World
PDF
DNT Brochure 2025 – ISV Solutions @ D365
PPTX
Why Generative AI is the Future of Content, Code & Creativity?
PDF
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
Microsoft Office 365 Crack Download Free
PPTX
Patient Appointment Booking in Odoo with online payment
PDF
Cost to Outsource Software Development in 2025
PPTX
Computer Software - Technology and Livelihood Education
PDF
MCP Security Tutorial - Beginner to Advanced
Modern SharePoint Intranet Templates That Boost Employee Engagement in 2025.docx
Tech Workshop Escape Room Tech Workshop
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
Oracle Fusion HCM Cloud Demo for Beginners
EaseUS PDF Editor Pro 6.2.0.2 Crack with License Key 2025
How Tridens DevSecOps Ensures Compliance, Security, and Agility
assetexplorer- product-overview - presentation
Ableton Live Suite for MacOS Crack Full Download (Latest 2025)
Weekly report ppt - harsh dattuprasad patel.pptx
The Dynamic Duo Transforming Financial Accounting Systems Through Modern Expe...
Cybersecurity: Protecting the Digital World
DNT Brochure 2025 – ISV Solutions @ D365
Why Generative AI is the Future of Content, Code & Creativity?
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
Computer Software and OS of computer science of grade 11.pptx
Microsoft Office 365 Crack Download Free
Patient Appointment Booking in Odoo with online payment
Cost to Outsource Software Development in 2025
Computer Software - Technology and Livelihood Education
MCP Security Tutorial - Beginner to Advanced
Ad

SQL Presentation-1 yehjebjj yeuu helo the worls.ppt

  • 1. Chapter 7: SQL, the Structured Query Language Soid Quintero & Ervi Bongso CS157B
  • 2. Overview Introduction DDL Commands DML Commands SQL Statements, Operators, Clauses Aggregate Functions
  • 3. The ANSI standard language for the definition and manipulation of relational database. Includes data definition language (DDL), statements that specify and modify database schemas. Includes a data manipulation language (DML), statements that manipulate database content. Structured Query Language (SQL)
  • 4. Some Facts on SQL SQL data is case-sensitive, SQL commands are not. First Version was developed at IBM by Donald D. Chamberlin and Raymond F. Boyce. [SQL] Developed using Dr. E.F. Codd's paper, “A Relational Model of Data for Large Shared Data Banks.” SQL query includes references to tuples variables and the attributes of those variables
  • 5. SQL: DDL Commands CREATE TABLE: used to create a table. ALTER TABLE: modifies a table after it was created. DROP TABLE: removes a table from a database.
  • 6. SQL: CREATE TABLE Statement Things to consider before you create your table are: The type of data the table name what column(s) will make up the primary key the names of the columns CREATE TABLE statement syntax: CREATE TABLE <table name> ( field1 datatype ( NOT NULL ), field2 datatype ( NOT NULL ) );
  • 8. SQL: ALTER TABLE Statement To add or drop columns on existing tables. ALTER TABLE statement syntax: ALTER TABLE <table name> ADD attr datatype; or DROP COLUMN attr;
  • 9. SQL: DROP TABLE Statement Has two options: CASCADE: Specifies that any foreign key constraint violations that are caused by dropping the table will cause the corresponding rows of the related table to be deleted. RESTRICT: blocks the deletion of the table of any foreign key constraint violations would be created. DROP TABLE statement syntax: DROP TABLE <table name> [ RESTRICT|CASCADE ];
  • 10. Example: CREATE TABLE FoodCart ( date varchar(10), food varchar(20), profit float ); ALTER TABLE FoodCart ( ADD sold int ); ALTER TABLE FoodCart( DROP COLUMN profit ); DROP TABLE FoodCart; profit food date sold profit food date sold food date FoodCart FoodCart FoodCart
  • 11. SQL: DML Commands INSERT: adds new rows to a table. UPDATE: modifies one or more attributes. DELETE: deletes one or more rows from a table.
  • 12. SQL: INSERT Statement To insert a row into a table, it is necessary to have a value for each attribute, and order matters. INSERT statement syntax: INSERT into <table name> VALUES ('value1', 'value2', NULL); Example: INSERT into FoodCart VALUES (’02/26/08', ‘pizza', 70 ); FoodCart 70 pizza 02/26/08 500 hotdog 02/26/08 350 pizza 02/25/08 sold food date 500 hotdog 02/26/08 350 pizza 02/25/08 sold food date
  • 13. SQL: UPDATE Statement To update the content of the table: UPDATE statement syntax: UPDATE <table name> SET <attr> = <value> WHERE <selection condition>; Example: UPDATE FoodCart SET sold = 349 WHERE date = ’02/25/08’ AND food = ‘pizza’; FoodCart 70 pizza 02/26/08 500 hotdog 02/26/08 350 pizza 02/25/08 sold food date 70 pizza 02/26/08 500 hotdog 02/26/08 349 pizza 02/25/08 sold food date
  • 14. SQL: DELETE Statement To delete rows from the table: DELETE statement syntax: DELETE FROM <table name> WHERE <condition>; Example: DELETE FROM FoodCart WHERE food = ‘hotdog’; FoodCart Note: If the WHERE clause is omitted all rows of data are deleted from the table. 70 pizza 02/26/08 500 hotdog 02/26/08 349 pizza 02/25/08 sold food date 70 pizza 02/26/08 349 pizza 02/25/08 sold food date
  • 15. SQL Statements, Operations, Clauses SQL Statements: Select SQL Operations: Join Left Join Right Join Like SQL Clauses: Order By Group By Having
  • 16. SQL: SELECT Statement A basic SELECT statement includes 3 clauses SELECT <attribute name> FROM <tables> WHERE <condition> SELECT Specifies the attributes that are part of the resulting relation FROM Specifies the tables that serve as the input to the statement WHERE Specifies the selection condition, including the join condition. Note: that you don't need to use WHERE
  • 17. Using a “*” in a select statement indicates that every attribute of the input table is to be selected. Example: SELECT * FROM … WHERE …; To get unique rows, type the keyword DISTINCT after SELECT. Example: SELECT DISTINCT * FROM … WHERE …; SQL: SELECT Statement (cont.)
  • 18. Example: Person 80 34 Peter 54 54 Helena 70 29 George 64 28 Sally 80 34 Harry Weight Age Name 80 34 Peter 54 54 Helena 80 34 Harry Weight Age Name 80 54 80 Weight 1) SELECT * FROM person WHERE age > 30; 2) SELECT weight FROM person WHERE age > 30; 3) SELECT distinct weight FROM person WHERE age > 30; 54 80 Weight
  • 19. SQL: Join operation A join can be specified in the FROM clause which list the two input relations and the WHERE clause which lists the join condition. Example: Biotech 1003 Sales 1002 IT 1001 Division ID TN 1002 MA 1001 CA 1000 State ID Emp Dept
  • 20. SQL: Join operation (cont.) Sales 1002 IT 1001 Dept.Division Dept.ID TN 1002 MA 1001 Emp.State Emp.ID inner join = join SELECT * FROM emp join dept (or FROM emp, dept) on emp.id = dept.id;
  • 21. SQL: Join operation (cont.) IT 1001 Sales 1002 null null Dept.Division Dept.ID CA 1000 TN 1002 MA 1001 Emp.State Emp.ID left outer join = left join SELECT * FROM emp left join dept on emp.id = dept.id;
  • 22. SQL: Join operation (cont.) Sales 1002 Biotech 1003 IT 1001 Dept.Division Dept.ID MA 1001 null null TN 1002 Emp.State Emp.ID right outer join = right join SELECT * FROM emp right join dept on emp.id = dept.id;
  • 23. SQL: Like operation Pattern matching selection % (arbitrary string) SELECT * FROM emp WHERE ID like ‘%01’;  finds ID that ends with 01, e.g. 1001, 2001, etc _ (a single character) SELECT * FROM emp WHERE ID like ‘_01_’;  finds ID that has the second and third character as 01, e.g. 1010, 1011, 1012, 1013, etc
  • 24. SQL: The ORDER BY Clause Ordered result selection desc (descending order) SELECT * FROM emp order by state desc  puts state in descending order, e.g. TN, MA, CA asc (ascending order) SELECT * FROM emp order by id asc  puts ID in ascending order, e.g. 1001, 1002, 1003
  • 25. SQL: The GROUP BY Clause The function to divide the tuples into groups and returns an aggregate for each group. Usually, it is an aggregate function’s companion SELECT food, sum(sold) as totalSold FROM FoodCart group by food; FoodCart 419 pizza 500 hotdog totalSold food 70 pizza 02/26/08 500 hotdog 02/26/08 349 pizza 02/25/08 sold food date
  • 26. SQL: The HAVING Clause The substitute of WHERE for aggregate functions Usually, it is an aggregate function’s companion SELECT food, sum(sold) as totalSold FROM FoodCart group by food having sum(sold) > 450; FoodCart 500 hotdog totalSold food 70 pizza 02/26/08 500 hotdog 02/26/08 349 pizza 02/25/08 sold food date
  • 27. SQL: Aggregate Functions Are used to provide summarization information for SQL statements, which return a single value. COUNT(attr) SUM(attr) MAX(attr) MIN(attr) AVG(attr) Note: when using aggregate functions, NULL values are not considered, except in COUNT(*) .
  • 28. SQL: Aggregate Functions (cont.) COUNT(attr) -> return # of rows that are not null Ex: COUNT(distinct food) from FoodCart; -> 2 SUM(attr) -> return the sum of values in the attr Ex: SUM(sold) from FoodCart; -> 919 MAX(attr) -> return the highest value from the attr Ex: MAX(sold) from FoodCart; -> 500 70 pizza 02/26/08 500 hotdog 02/26/08 349 pizza 02/25/08 sold food date FoodCart
  • 29. SQL: Aggregate Functions (cont.) MIN(attr) -> return the lowest value from the attr Ex: MIN(sold) from FoodCart; -> 70 AVG(attr) -> return the average value from the attr Ex: AVG(sold) from FoodCart; -> 306.33 Note: value is rounded to the precision of the datatype 70 pizza 02/26/08 500 hotdog 02/26/08 349 pizza 02/25/08 sold food date FoodCart
  • 30. Riccardi, Greg. Principles of Database Systems with Internet and Java Applications. Addison Wesley, 2001. Ronald R. Plew, Ryan K. Stephens. Teach Yourself SQL in 24 Hours 3rd Edition. Sams Publishing, 2003. SQL http://en.wikipedia.org/wiki/SQL W3C http://www.w3schools.com/sql/sql_tryit.asp Wikipedia - SQL http://en.wikipedia.org/wiki/SQL Wikipedia - join http://en.wikipedia.org/wiki/Join_(SQL) References