Data Query Language (DQL) (Slides) - 2
Data Query Language (DQL) (Slides) - 2
2
Data Query Language
● Get data where the Price is more than $0.22. 2006-01-15 Excellent 77 0.1796
● Get all data for the month of .February.. 2006-02-15 Low 21 0.2299
2006-02-15 Good 61 0.2299
● Sort data based on Quality.
2006-02-15 Fair 34 0.2499
● Group data together using Market_ID.
2006-02-15 Fair 28 0.1839
● Change dates to days of the week.
2006-03-15 High 67 0.236
● Change the price into another currency.
2006-03-15 Good 82 0.235
2006-03-15 Excellent 24 0.205
2006-03-15 High 28 0.185
2006-04-15 Low 56 0.24
DQL is a set of SQL commands that allows us to
2006-04-15 Excellent 82 0.238
filter, organize, and retrieve specific data based on
2006-04-15 Good 75 0.2321
various criteria. This is known as querying a
2006-04-15 Good 75 0.2321
database.
2006-05-15 Fair 67 0.2798
Data Query Language
| The SELECT statement retrieves a column, list of columns, or all columns from a specific
table in a specific database and outputs a results set.
When we use SELECT, we're not creating a new table in our database, but rather generating a temporary, read-only
results set that contains the data we've requested.
Data Query Language
Syntax: SELECT col(s) FROM Database.Table_3; will retrieve data only from Table_3:
Database (db)
Table_1 Table_2 Table_3
col1 col2 col3 col1 col2 col3 col1 col2 col3
x 34 s z 68 s x 7 s
y 73 m x 1 l x 42 s
z 22 l w 7 l z 9 s
w 12 m y 56 m x 7 s
Note: These are bad naming examples, but we use them here to show how queries work. Always try to use descriptive titles.
Data Query Language
Table_1 Results
col1 col2 col3 col1
x 34 s SELECT x
col1
y 73 m FROM y
z 22 l db.Table_1; z
w 12 m w
In our results set, we have retrieved data from a single column, col1.
Data Query Language
Table_1 Results
col1 col2 col3 col1 col3
SELECT
x 34 s col1, x s
col3
y 73 m y m
FROM
z 22 l db.Table_1; z l
w 12 m w m
In this example, a list of columns (col1, col3) is specified. They can be in any order and are separated by
commas. Our result has data from col1 and col3.
Data Query Language
Table_1 Results
col1 col2 col3 col1 col2 col3
SELECT
x 34 s x 34 s
*
y 73 m FROM y 73 m
db.Table_1;
z 22 l z 22 l
w 12 m w 12 m
Using the asterisk (*) with SELECT acts as a wildcard and represents all columns. Using SELECT * with
Table_1, we retrieve all columns from Table_1 in our results set.
Data Query Language
LIMIT
| LIMIT restricts the number of rows that are returned in the results set.
Table_1 Results
col1 col2 col3 col1 col2 col3
SELECT
x 34 s * x 34 s
FROM
y 73 m y 73 m
db.Table_1
z 22 l LIMIT 2;
w 12 m
Tables may have thousands or millions of records, so limiting the number of records allows you to quickly
view your results.
Data Query Language
SELECT DISTINCT
Syntax: SELECT DISTINCT col(s) FROM db.table;
When working with databases, we often encounter
duplicate data. Sometimes, this repetition can be useful or
Table
necessary. However, there are instances where we want to
eliminate duplicates to gain a clearer picture of our data. -- -- -- --
-- -- -- --
This is where the SELECT DISTINCT keyword is used.
-- -- -- --
-- -- -- --
SELECT DISTINCT eliminates rows where data are -- -- -- --
Using DISTINCT
| DISTINCT is used with SELECT to only return unique (non-duplicated) values in a specified
column.
Table_2 Results
col1 col2 col3 col3
SELECT DISTINCT
z 68 s s
col3
x 1 l FROM l
db.Table_2;
w 7 l m
y 56 m
Since ‘l’ appears in col3 twice, only the unique values, ‘s’, ‘m’, and ‘l’ in col3 are listed, with the additional ‘l’ removed.
Note: Only the specified column is returned.
Data Query Language
|
When used with multiple columns, the DISTINCT keyword returns unique combinations of
values across all the listed columns. If the same combination of values appears in more
than one row, only one distinct row is returned.
Table_3 Results
SELECT DISTINCT
col1 col2 col3 col1 col3
col1,
x 7 s 1 col3 x s 1
x 42 s 2 FROM z s 2
z 9 s 3 db.Table_3;
x 7 s 4
col1 col3
X s 1
Step 1: SELECT only col1 and col3. Step 2: Only one distinct copy of
x s 2
DISTINCT checks these two columns each unique combination is
z s 3
for pairs of unique combinations. x included. (x and s) and (z and s)
x s 4
and s are unique; z and s are unique. are both unique combinations.
Data Query Language
DISTINCT *
| When used with *, DISTINCT removes all rows that are exact duplicates.
Table_3 Results
col1 col2 col3 col1 col2 col3
x 7 s 1 SELECT DISTINCT x 7 s 1
*
x 42 s 2 x 42 s 2
FROM
z 9 s 3 db.Table_3; z 9 s 3
x 7 s 4
● Rows 1 and 4 have identical values in col1, col2, and col3, so they are duplicates.
● While rows 1, 2and 4 all have x in col1 and s in col3, they have different values in col2, so the combination of x, 42,
and s in row 2 is different to x, 7, and s.
Data Query Language
DISTINCT
While DISTINCT is a powerful tool for
managing duplicate data, it can slow
down your query if used on large tables.
WHERE
|
The WHERE clause in SQL is another powerful tool for filtering our data where a specific
condition is TRUE. WHERE filters the results to include only the rows where the value in a
column equals <value>.
Table Results
In this example, only the rows where col4 = |||| are 4 4
in our results, while rows with col4 = |||| and |||| are -- -- -- -- -- -- -- --
-- -- -- -- -- -- -- --
discarded. -- -- -- -- -- -- -- --
-- -- -- -- -- -- -- --
| WHERE checks if a condition is TRUE for a row, and only returns the rows that meet that
condition. A condition consists of three parts:
The name of the column that the This is how we want to compare The specific value we want to
condition should apply to. the values in the column to a compare the column values to.
specific value.
A condition can only be applied to <value> can be a number, text,
one column at a time. We can SQL offers many other comparison another column, or even another
combine conditions by using logic operators (<, >, <=, >=, !=). query. Each of these options has a
operators like AND, OR, and NOT. specific syntax.
Data Query Language
Using WHERE
Table_2 Results
col1 col2 col3 col1 col2
1 SELECT FALSE 1
z 68 s
col1,
col2 TRUE
x 1 l 2 x 1 2
FROM FALSE
w 7 l 3 db.Table_2; 3
y 56 m
WHERE FALSE
4 col2 = 1 -- Condition 4