8/4/2019 SELECT Statement S2
http://slidepdf.com/reader/full/select-statement-s2 1/25
Saturday, April 14, 2012 MySQL- DML & DCL 1
Inserting, Updating and
Deleting Records in MySQL
SELECT Statement
@Instructor by Ngo Trung Kien
Date: Thursday, September 09, 2010RDBMS and Data Management
8/4/2019 SELECT Statement S2
http://slidepdf.com/reader/full/select-statement-s2 2/25
Saturday, April 14, 2012 MySQL- DML & DCL 2
Inserting Records in MySQL
Inserting
Inserting Multiple Rows
Inserting SET
INSERT INTO table_name (List
field,…,) VALUES(Value1,Value2,…,);
INSERT INTO table_name (List field,…,) VALUES
(Value1,Value2,…,), (Value1,Value2,…,),
(Value1,Value2,…,),. . . ;
INSERT INTO table_name
SET Field_1= value_1, Field_2=value_2,…,;
8/4/2019 SELECT Statement S2
http://slidepdf.com/reader/full/select-statement-s2 3/25
Saturday, April 14, 2012 MySQL- DML & DCL 3
Updating, DELETE Data in MySQL
UPDATE table_name
SET field = value [,field = value]
[WHERE conditions];
DELETE FROM <table_name>
WHERE <conditions>;
8/4/2019 SELECT Statement S2
http://slidepdf.com/reader/full/select-statement-s2 4/25
Saturday, April 14, 2012 MySQL- DML & DCL 4
Variable in T-SQL
SyntaxLocal
DECLARE{ @local_name [as] Data_type,
@local_name [as] Data_type,...}
local_name: Name of variable data_type: is a data type of system or user define
Example
DECLARE @student_id char[10]
DECLARE @proId int
Global: that are defined and maintained by the sytems.@@VERSION
8/4/2019 SELECT Statement S2
http://slidepdf.com/reader/full/select-statement-s2 5/25
Saturday, April 14, 2012 MySQL- DML & DCL 5
Comments in SQL
The standard SQL comment is two hyphens (--).However, some databases use other forms of commentsas shown in the table below.
8/4/2019 SELECT Statement S2
http://slidepdf.com/reader/full/select-statement-s2 6/25
Saturday, April 14, 2012 MySQL- DML & DCL 6
Introduction
The SELECT statement is discussed in order to retrieve andaccess data from database
That data that is retrieved is not only useful for the purpose ofreading but also for modification by operator as UPDATE,DELETE,…
In a table data can be viewed using the SELECT statement willdisplay the required information in table
The SELECT statement retrieves rows and columns from one ormore table
The Ouput of the SELECT statement is another table calledresult set
The columns appear in the same sequence as the order ofexpression in the SELECT statement.
8/4/2019 SELECT Statement S2
http://slidepdf.com/reader/full/select-statement-s2 7/25
Saturday, April 14, 2012 MySQL- DML & DCL 7
Classifying SELECT statement
The SELECT statement also joins two table,or retrieves a subset of columns from one ormore tables
The SELECT statement also defines thecolums to be used for a query
The SELECT statement can consist of aseries of expressions seperates by commas
8/4/2019 SELECT Statement S2
http://slidepdf.com/reader/full/select-statement-s2 8/25
Saturday, April 14, 2012 MySQL- DML & DCL 8
Syntax SELECT statement
Syntax:
SELECT <collection>
[FROM <table name>][ WHERE <expression>]
[GROUP BY <expression> HAVING
<Condition>][ORDER BY <Field_name> ASC| DESC]
[LIMIT <[Offset,] Limit>]
Which:
collection: list of column name in any table (include between columns “,” if retrieves than one) or any (function, variable,..) table name: List of tabl
Expression: Operators and comparison
8/4/2019 SELECT Statement S2
http://slidepdf.com/reader/full/select-statement-s2 9/25
Saturday, April 14, 2012 MySQL- DML & DCL 9
Full SELECT statement
8/4/2019 SELECT Statement S2
http://slidepdf.com/reader/full/select-statement-s2 10/25
Saturday, April 14, 2012 MySQL- DML & DCL 10
Example SELECT statement
SELECT * FROM tblCategories
SELECT cat_id
,cat_name
,cat_img
FROM tblCategories SELECT c.cat_id as ID
,c.cat_name as category_name
FROM tblCategories as c
WHERE c.cat_id=‘PA001’
SELECT LEFT(‘C0909K’,3)
8/4/2019 SELECT Statement S2
http://slidepdf.com/reader/full/select-statement-s2 11/25
Saturday, April 14, 2012 MySQL- DML & DCL 11
SELECT INTO
INTO clause creates a new table and insertrows, colums listed in the SELECT
INTO clause inserts existing rows into the
new table SELECT <list colums>
INTO <new table>
FROM <table selected>
SELECT * INTO backup_tblCategories
FROM tblCategories
8/4/2019 SELECT Statement S2
http://slidepdf.com/reader/full/select-statement-s2 12/25
Saturday, April 14, 2012 MySQL- DML & DCL 12
SELECT DISTINCT
DISTINCT keyword elimitnates rows that arerepeateing from the result set of a SELECTstatement
Syntax:SELECT DISTINCT <column eliminate>FROM <table name>
Ex: SELECT DISTINCT cat_name FROM
tblCategories
8/4/2019 SELECT Statement S2
http://slidepdf.com/reader/full/select-statement-s2 13/25
Saturday, April 14, 2012 MySQL- DML & DCL 13
SELECT WHERE
WHERE used to conditionally select or limit records retrieved bythe query
Syntax:SELECT <collection>
FROM <table name>
WHERE <expression>
Where:
collection: list of column name in any table (include
between columns “,” if retrieves than one) or any(function, variable,..)
table name: List of table
Expression: Operators
8/4/2019 SELECT Statement S2
http://slidepdf.com/reader/full/select-statement-s2 14/25
Saturday, April 14, 2012 MySQL- DML & DCL 14
SELECT WHERE- Expression
The different operators that can be used with theWHERE <Expression>
Operator Description
=,>=,<,>,<= Compare operators
< >, != Not equal to
AND, OR, NOT WHERE price>=10.5 AND price<=20.5 ORprice=21.0
LIKE Search for pattern ‘%[_^string]%’
BETWEEN WHERE price BETWEEN 10.5 AND 20.5
IN,NOT IN, ALL, ANY „Hellochao‟ IN („C‟, „Hello‟, „chao‟)
8/4/2019 SELECT Statement S2
http://slidepdf.com/reader/full/select-statement-s2 15/25
Saturday, April 14, 2012 MySQL- DML & DCL 15
Options and Attributes
8/4/2019 SELECT Statement S2
http://slidepdf.com/reader/full/select-statement-s2 16/25
Saturday, April 14, 2012 MySQL- DML & DCL 16
SELECT ORDER BY, GROUP BY
ORDER BY: Sort query result by one or more columnsfollow ASC, DESC
GROUP BY: clause partition the result set into one ormore subset, can be it uses with HAVING
Syntax:
SELECT <collection>FROM <table name>WHERE <expression>
ORDER BY <column ASC|DESC>GROUP BY <column> [HAVING <expression>]
8/4/2019 SELECT Statement S2
http://slidepdf.com/reader/full/select-statement-s2 17/25
Saturday, April 14, 2012 MySQL- DML & DCL 17
Example ORDER , GROUP BY
SELECT * FROM tblCategories as c
WHERE 1=1 ORDER BY c.cat_name ASC
SELECT ord_id, cus_name
, sum(pro_price*ord_quantity) as „Totals‟
FROM tblOrderDetails
WHERE 1=1GROUP BY cus_id
8/4/2019 SELECT Statement S2
http://slidepdf.com/reader/full/select-statement-s2 18/25
Saturday, April 14, 2012 MySQL- DML & DCL 18
SELECT - The LIMIT Clause
The LIMIT clause is used most effectively in a SELECT statement
when it is used with an ORDER BY clause. The LIMIT clause takes two arguments, as the following syntax
shows:
LIMIT [<offset>,] <row count> SELECT * FROM tblCategories as c LIMIT 10
SELECT * FROM tblCategories as c LIMIT 3,5
SELECT title, rating, rental_rate
FROM film
ORDER BY title ASC
LIMIT 5;
8/4/2019 SELECT Statement S2
http://slidepdf.com/reader/full/select-statement-s2 19/25
Saturday, April 14, 2012 MySQL- DML & DCL 19
Join multi table by key
In a normalized database, groups of data are stored inindividual tables, and relationships are established betweenthose tables to link related data
As a result, often when creating SELECT, UPDATE, orDELETE statements, you want to be able to access data in
different tables to carry out an operation affected by thoserelationships
The real power of a relational database emerges from the abilityto combine multiple entities in a single operation
8/4/2019 SELECT Statement S2
http://slidepdf.com/reader/full/select-statement-s2 20/25
Saturday, April 14, 2012 MySQL- DML & DCL 20
Using WHERE
Syntax:SELECT <collection columns>
FROM <table_1, table_2>
WHERE <table_1.c_key=table_2.c_key>
c_key: is primary key or foreign
key
Table_1, table_2: Related 1-1, 1-N
or M-N
8/4/2019 SELECT Statement S2
http://slidepdf.com/reader/full/select-statement-s2 21/25
Saturday, April 14, 2012 MySQL- DML & DCL 21
Example using WHERE
SELECT C.ContactName
,C.City
,O.OrderDate
O.RequiredDate
FROM Customers as C, Orders as O
WHERE C.CustomerID=O.CustomerID
8/4/2019 SELECT Statement S2
http://slidepdf.com/reader/full/select-statement-s2 22/25
Saturday, April 14, 2012 MySQL- DML & DCL 22
Using Join
SQL supports the following JOIN syntaxes forthe table references part of SELECTstatements and multiple-table DELETE and
UPDATE statements: Table references type:
INNER JOIN
LEFT JOIN
RIGHT JOIN
8/4/2019 SELECT Statement S2
http://slidepdf.com/reader/full/select-statement-s2 23/25
Saturday, April 14, 2012 MySQL- DML & DCL 23
Using Join[1]
SyntaxSELECT <collection columns>
FROM <table_1>
INNER|LEFT|RIGHT JOIN <table_2>
ON <table_1.c_key=table_2.c_key>
c_key: is primary key or foreign key
Table_1, table_2: Related 1-1, 1-N or M-N
8/4/2019 SELECT Statement S2
http://slidepdf.com/reader/full/select-statement-s2 24/25
Saturday, April 14, 2012 MySQL- DML & DCL 24
Example using Join
SELECT C.ContactName,C.City
,O.OrderDate
O.RequiredDateFROM Customers as C
INNER JOIN Orders as O
ON C.CustomerID=O.CustomerID