0% found this document useful (0 votes)
3 views18 pages

Data Query Language (DQL) (Slides) - 2

Data Query Language (DQL) is used to communicate with databases for data extraction and manipulation, primarily through the SELECT and WHERE keywords. The document outlines various SQL commands for querying data, including retrieving specific columns, filtering results, and eliminating duplicates. It emphasizes the importance of organizing and cleaning data to enhance its quality and usability.

Uploaded by

tesami315
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views18 pages

Data Query Language (DQL) (Slides) - 2

Data Query Language (DQL) is used to communicate with databases for data extraction and manipulation, primarily through the SELECT and WHERE keywords. The document outlines various SQL commands for querying data, including retrieving specific columns, filtering results, and eliminating duplicates. It emphasizes the importance of organizing and cleaning data to enhance its quality and usability.

Uploaded by

tesami315
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Querying with SQL

Data Query Language


Please do not copy without permission. © ALX 2024.
Data Query Language

Data Query Language

Data Query Language (DQL) allows us to


communicate with databases, facilitating
precise extraction and manipulation of data.

The SELECT keyword is used to retrieve data from


a database, essentially forming the foundation of
any data extraction query.

The WHERE keyword is used to retrieve data


conditionally, enabling more precise and
targeted data extraction.

2
Data Query Language

Querying data with SQL


Date Quality Market_ID Price ($)
Here we have a table of data. Some data may be
2006-01-15 High 11 0.2235
more relevant to us than others. For example, we
2006-01-15 Low 56 0.2145
may want to:
2006-01-15 Good 24 0.2055

● 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

Querying with SQL


SELECT retrieves data from a database. We can use
tools like filters and functions with SELECT to
manipulate, clean, and organize data.

Manipulating or changing data adds value to data


by transforming data or extracting more SELECT
information from existing data.

Cleaning data improves the quality of our data.

Change Clean Organize


Organizing data makes it simpler to work with and
understand.

Together, these tools help us to understand data.


Data Query Language

How to query data: SELECT

| 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.

SELECT col(s) FROM database_name.table_name;

Keyword: Identifier: Keyword: Identifier:


Get data from the Can be a column Always specify which Name of the database
database. name, list of columns, table to query. followed by “.” and then
or * to select all the name of the table.
columns.

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

How to query data: FROM

| FROM specifies which table to query fromDatabase


since databases often have more than one table.

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

SELECT one column

| SELECT can be used to retrieve data from a single column.

Syntax: SELECT col FROM db.table;

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

SELECT multiple columns

| SELECT can retrieve data from a comma-separated list of columns.

Syntax: SELECT col1, col2, … FROM db.table;

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

SELECT all columns

| SELECT * retrieves data from all the columns available in a table.

Syntax: SELECT * FROM db.table;

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.

Syntax: SELECT * FROM db.table LIMIT number of rows;

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 -- -- -- --

duplicated. Like SELECT, we can check if data in specific


column(s) or all columns are duplicated, and remove SELECT
them. DISTINCT

SELECT DISTINCT doesn’t change the original


Results
table, it only affects the query’s results.
-- -- -- --
-- -- -- --
-- -- -- --
A common use of SELECT DISTINCT is to remove
all duplicates from a table, using * or from a -- -- -- --
specific set of columns. -- -- -- --
Data Query Language

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

DISTINCT on multiple columns

|
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.

SQL has to compare every row with every


other row to identify duplicates, which can
be time-consuming.

So use DISTINCT with care, when the need


to remove duplicates from our results set
outweighs the potential performance cost.
Data Query Language

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. -- -- -- -- -- -- -- --
-- -- -- -- -- -- -- --

Essentially, WHERE acts as a gatekeeper, deciding -- -- -- --


-- -- -- --
which rows from the original table meet the
condition and should be included in the query
result. WHERE TRUE
col4 = ||||
For example, in a medical clinic, we may want to only
Discarded
see the data where patients are currently admitted, FALSE 4

or we may want the data where patients are -- -- -- --


-- -- -- --
children.
Data Query Language

WHERE and conditions

| 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:

Syntax: SELECT col(s) FROM db.table WHERE col_to_filter = value;

Column to filter on Operator Value

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

| WHERE only includes rows where the given condition is TRUE.

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

Only col1 and col2 are selected in our results set.


The only row where the condition is TRUE is row 2, so it is included.

You might also like