09-Intro To SQL
09-Intro To SQL
SQL
(Introduction and Basic Queries)
➔
S E LE CT
➔
B a s ic F o rm database schema creation and modification
➔
C o n ditio n s
➔
S o rtin g SQL is an acronym for
➔
J o in in g t w o
ta b le s S tructured Q uery L anguage
➔
S e a rc h in g
➔
L o g ic a l
O pe ra to rs
➔
Ma th e m a t ic
a l O pe ra to rs
➔
S trin g
F u n c t io n s
➔
G ro u pin g o f
ro w s
➔
N e s t in g o f
S E LE CT
S ta te m e n t s WAHAJ Fundamentals of Data Base Systems March 20th 2014
History of SQL
First implementation in the beginning of the
L e c tu re 9 1970s
S QL
Standardization in 1986 by ANSI and in 1987
➔
➔
His t o ry
➔
S E LE CT by ISO
➔
B a s ic F o rm
➔
C o n ditio n s SQL-86
➔
S o rtin g
➔
J o in in g t w o Further revisions of the ISO standard:
ta b le s
➔
S e a rc h in g SQL-89
➔
L o g ic a l
O pe ra to rs SQL-92
➔
Ma th e m a t ic
a l O pe ra to rs S Q L -9 9
S trin g
SQL:2003
➔
F u n c t io n s
G ro u pin g o f
SQL:2006
➔
ro w s
SQL:2008
➔
N e s t in g o f
S E LE CT
S ta te m e n t s WAHAJ Fundamentals of Data Base Systems March 20th 2014
Retrieval Queries in SQL
L e c tu re 9
SQL has one basic statement for retrieving
information from a database; the S E L E C T
S QL
statement
➔
➔
His t o ry
➔
S E LE CT Important distinction between SQL and the formal
B a s ic F o rm
relational model:
➔
➔
C o n ditio n s
S o rtin g
SQL allows a table (relation) to have two or more
➔
➔
J o in in g t w o
ta b le s tuples that are identical in all their attribute values
S e a rc h in g
Hence, an SQL relation (table) is a m u lti-s e t of
➔
➔
L o g ic a l
O pe ra to rs tuples
Ma th e m a t ic
SQL relations can be constrained to be sets by
➔
a l O pe ra to rs
➔
S trin g specifying a PRIMARY KEY
F u n c t io n s
➔
G ro u pin g o f
ro w s
➔
N e s t in g o f
S E LE CT
S ta te m e n t s WAHAJ Fundamentals of Data Base Systems March 20th 2014
Basic form of SELECT
L e c tu re 9
Basic form of the SQL SELECT statement
S QL
<attribute list>
➔
➔
His t o ry
S E LE CT
➔
S E LE CT F ROM <table list>
➔
B a s ic F o rm
➔
C o n ditio n s
➔
S o rtin g <attribute list> is a list of attribute names whose
➔
J o in in g t w o values are to be retrieved by the query
ta b le s
➔
S e a rc h in g <table list> is a list of the relation names required
➔
L o g ic a l to process the query
O pe ra to rs
➔
Ma th e m a t ic
a l O pe ra to rs
➔
S trin g
F u n c t io n s
➔
G ro u pin g o f
ro w s
➔
N e s t in g o f
S E LE CT
S ta te m e n t s WAHAJ Fundamentals of Data Base Systems March 20th 2014
Basic form of SELECT – Example
E x a m p le fo r C S N D B :
L e c tu re 9
➔
S QL S E LE CT Title, Author
➔
His t o ry
➔
S E LE CT F ROM Article;
➔
B a s ic F o rm
➔
C o n ditio n s
➔
S o rtin g You can use * to display all attributes of a table:
➔
J o in in g t w o SELECT Name, Description FROM Topic;
ta b le s
is th e s a m e a s :
➔
S e a rc h in g
➔
L o g ic a l SELECT * FROM TOPIC;
O pe ra to rs
➔
Ma th e m a t ic
a l O pe ra to rs
➔
S trin g
F u n c t io n s
➔
G ro u pin g o f
ro w s
➔
N e s t in g o f
S E LE CT
S ta te m e n t s WAHAJ Fundamentals of Data Base Systems March 20th 2014
Basic form of SELECT with condition
L e c tu re 9
Basic form of the SQL SELECT statement is
called a m a p p in g or a SELECT-FROM-WHERE
➔
S QL b lo c k
➔
His t o ry
➔
S E LE CT
➔
B a s ic F o rm S E LE CT <attribute list>
C o n ditio n s
F ROM <table list>
➔
➔
S o rtin g
➔
J o in in g t w o WHE R E <condition>
ta b le s
➔
S e a rc h in g
➔
L o g ic a l <condition> is a conditional (Boolean) expression
O pe ra to rs
that identifies the tuples to be retrieved by the
query
➔
Ma th e m a t ic
a l O pe ra to rs
➔
S trin g
F u n c t io n s
➔
G ro u pin g o f
ro w s
➔
N e s t in g o f
S E LE CT
S ta te m e n t s WAHAJ Fundamentals of Data Base Systems March 20th 2014
Basic form of SELECT with condition
Example
E x a m p le fo r C S N D B :
L e c tu re 9
➔
S QL S E LE CT Title
➔
His t o ry
➔
S E LE CT F ROM Article
B a s ic F o rm
WHE R E Author='wahaj';
➔
➔
C o n ditio n s
➔
S o rtin g
J o in in g t w o
More than one condition can be expressed by
➔
ta b le s
➔
S e a rc h in g using logical operators such as A N D and O R :
➔
L o g ic a l
Title
O pe ra to rs
➔
Ma th e m a t ic S E LE CT
a l O pe ra to rs F ROM Article
➔
S trin g
F u n c t io n s WHE R E Author='wahaj' A N D
➔
G ro u pin g o f
PublishStatus='0';
ro w s
➔
N e s t in g o f
S E LE CT
S ta te m e n t s WAHAJ Fundamentals of Data Base Systems March 20th 2014
Basic form of SELECT with condition
and order
L e c tu re 9
You can specify how the resulting table should
be ordered:
➔
S QL
➔
His t o ry
➔
S E LE CT S E LE CT <attribute list>
B a s ic F o rm
F ROM <table list>
➔
➔
C o n ditio n s
➔
S o rtin g WHE R E <condition>
J o in in g t w o
O R DE R B Y <attribute>
➔
ta b le s
➔
S e a rc h in g
➔
L o g ic a l
O pe ra to rs
➔
Ma th e m a t ic
a l O pe ra to rs
➔
S trin g
F u n c t io n s
➔
G ro u pin g o f
ro w s
➔
N e s t in g o f
S E LE CT
S ta te m e n t s WAHAJ Fundamentals of Data Base Systems March 20th 2014
Basic form of SELECT with condition
and order – Example
L e c tu re 9
You can specify how the resulting table should
be ordered:
➔
S QL
➔
His t o ry
➔
S E LE CT S E LE CT ID, Title
B a s ic F o rm
F ROM Article
➔
➔
C o n ditio n s
➔
S o rtin g WHE R E Author='sharifi'
J o in in g t w o
O R DE R B Y Title;
➔
ta b le s
➔
S e a rc h in g
➔
L o g ic a l
O pe ra to rs Compare the result to the same query without the
➔
Ma th e m a t ic O R DE R B Y part!
a l O pe ra to rs
➔
S trin g
F u n c t io n s
➔
G ro u pin g o f
ro w s
➔
N e s t in g o f
S E LE CT
S ta te m e n t s WAHAJ Fundamentals of Data Base Systems March 20th 2014
Joining two tables using the
SELECT-FROM-WHERE statement
The SELECT statement allows you to query
L e c tu re 9 more than one table at a time:
➔
S QL
➔
His t o ry S E LE CT Title, DisplayName
S E LE CT
F ROM Article, Author
➔
➔
B a s ic F o rm
➔
C o n ditio n s WHE R E Author = Username;
➔
S o rtin g
➔
J o in in g t w o
ta b le s When using more than two tables you should
always put the table name in front of the attribute
➔
S e a rc h in g
➔
L o g ic a l
O pe ra to rs name (this is mandatory of two tables have the
➔
Ma th e m a t ic same attribute name):
a l O pe ra to rs
➔
S trin g
F u n c t io n s S E LE CT Article.Title, Author.DisplayName
G ro u pin g o f
Article, Author
➔
ro w s F ROM
N e s t in g o f
WHE R E Article.Author = Author.Username;
➔
S E LE CT
S ta te m e n t s WAHAJ Fundamentals of Data Base Systems March 20th 2014
Joining two tables
ta b le s
S e a rc h in g
Every row in the first table is returned with all
➔
➔
L o g ic a l
O pe ra to rs rows of the second table, followed by the
Ma th e m a t ic
same thing for the second row in the first
➔
a l O pe ra to rs
➔
S trin g
table, and so on ...
F u n c t io n s
➔
G ro u pin g o f
ro w s
➔
N e s t in g o f
S E LE CT
S ta te m e n t s WAHAJ Fundamentals of Data Base Systems March 20th 2014
Search Functions
L e c tu re 9
The SELECT statement allows you to search:
S QL
Article.Title, Author.DisplayName
➔
➔
His t o ry
S E LE CT
➔
S E LE CT F ROM Article, Author
B a s ic F o rm
Article.Title L IK E “Welcome%”
➔
➔
C o n ditio n s WHE R E
➔
S o rtin g AN D Article.Author =
J o in in g t w o
Author.Username;
➔
ta b le s
➔
S e a rc h in g
L o g ic a l
The % sign matches any string.
➔
O pe ra to rs
➔
Ma th e m a t ic
a l O pe ra to rs
The _ sign matches any character.
➔
S trin g
F u n c t io n s
➔
G ro u pin g o f
ro w s
➔
N e s t in g o f
S E LE CT
S ta te m e n t s WAHAJ Fundamentals of Data Base Systems March 20th 2014
Logical Operators
L e c tu re 9
You can use logical operators in the condition:
S QL
Article.Title, Author.DisplayName
➔
➔
His t o ry S E LE CT
➔
S E LE CT F ROM Article, Author
➔
B a s ic F o rm
➔
C o n ditio n s WHE R E (Author.Username = 'khattab' O R
➔
S o rtin g Author.Username = 'sharifi')
A N D Article.Author =
➔
J o in in g t w o
ta b le s
➔
S e a rc h in g Author.Username;
➔
L o g ic a l
O pe ra to rs
➔
Ma th e m a t ic Another operator you can use is the N O T
➔
a l O pe ra to rs
S trin g
operator.
F u n c t io n s
➔
G ro u pin g o f
ro w s
➔
N e s t in g o f
S E LE CT
S ta te m e n t s WAHAJ Fundamentals of Data Base Systems March 20th 2014
Mathematical Operators
and String Functions
A lot of mathematical operators, string
L e c tu re 9
functions and other functions exist in MySQL
S QL
that can be used in the query:
➔
➔
His t o ry
S E LE CT
E x a m p le fo r C S N D B :
➔
➔
B a s ic F o rm
➔
C o n ditio n s Display the length of each article:
➔
S o rtin g
➔
J o in in g t w o
➔
ta b le s
S e a rc h in g
S E L E C T Title, LENGTH(Text) F R O M Article;
➔
L o g ic a l
O pe ra to rs
➔
Ma th e m a t ic The same, but adding the length of title:
a l O pe ra to rs
➔
S trin g
➔
F u n c t io n s
G ro u pin g o f
S E L E C T Title, ( LENGTH(Text) +
ro w s LENGTH(Title) ) F R O M Article;
➔
N e s t in g o f
S E LE CT
S ta te m e n t s WAHAJ Fundamentals of Data Base Systems March 20th 2014
Grouping of rows
➔
S E LE CT
➔
B a s ic F o rm counting the grouped rows:
➔
C o n ditio n s
➔
S o rtin g E x a m p le fo r C S N D B :
J o in in g t w o
Display the number of articles for each
➔
ta b le s
➔
➔
S e a rc h in g
L o g ic a l
author:
O pe ra to rs S E L E C T Author.DisplayName, COUNT(Article.ID)
➔
Ma th e m a t ic
a l O pe ra to rs F R O M Article, Author
➔
S trin g
F u n c t io n s WHE R E Author.Username = Article.Author
➔
G ro u pin g o f
ro w s G R O U P B Y Author.DisplayName;
➔
N e s t in g o f
S E LE CT
S ta te m e n t s WAHAJ Fundamentals of Data Base Systems March 20th 2014
Nesting SELECT statements
➔
S E LE CT
➔
B a s ic F o rm S h o w th e title s o f th e a rtic le s b y a ll a u th o rs
C o n ditio n s
w h o s e la s t n a m e is “k h a tta b ”:
➔
➔
S o rtin g
➔
J o in in g t w o S E L E C T Title
ta b le s
➔
S e a rc h in g F R O M Article
WHE R E Author IN
➔
L o g ic a l
O pe ra to rs
➔
Ma th e m a t ic
( S E L E C T Username
a l O pe ra to rs
➔
S trin g F R O M Author
F u n c t io n s
➔
G ro u pin g o f WHE R E DisplayName L IK E "%khattab"
ro w s
➔
N e s t in g o f
);
S E LE CT
S ta te m e n t s WAHAJ Fundamentals of Data Base Systems March 20th 2014
Summary of SQL Queries
➔
B a s ic F o rm
➔
C o n ditio n s
S o rtin g
S E LE CT <attribute list>
➔
➔
J o in in g t w o
ta b le s F ROM <table list>
S e a rc h in g
[WHE R E <condition>]
➔
➔
L o g ic a l
➔
O pe ra to rs
Ma th e m a t ic
[G R O U P B Y <grouping attribute(s)>]
a l O pe ra to rs [HA V IN G <group condition>]
➔
S trin g
F u n c t io n s [O R DE R B Y <attribute list>]
➔
G ro u pin g o f
ro w s
➔
N e s t in g o f
S E LE CT
S ta te m e n t s WAHAJ Fundamentals of Data Base Systems March 20th 2014
Summary of SQL Queries (contd.)
S E LE CT
queries
➔
➔
B a s ic F o rm
➔
C o n ditio n s The WHERE-clause specifies the conditions for selection
➔
S o rtin g and join of tuples from the relations specified in the
J o in in g t w o FROM-clause
➔
ta b le s
➔
S e a rc h in g GROUP BY specifies grouping attributes
➔
L o g ic a l HAVING specifies a condition for selection of groups
O pe ra to rs
➔
Ma th e m a t ic ORDER BY specifies an order for displaying the result of
a l O pe ra to rs a query
S trin g
A query is evaluated by first applying the WHERE-
➔
F u n c t io n s
➔
G ro u pin g o f clause, then GROUP BY and HAVING, and finally the
ro w s SELECT-clause
➔
N e s t in g o f
S E LE CT
S ta te m e n t s WAHAJ Fundamentals of Data Base Systems March 20th 2014