0% found this document useful (0 votes)
10 views44 pages

ETL SQL Training Wk2 Where

Uploaded by

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

ETL SQL Training Wk2 Where

Uploaded by

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

Filtering Results w/

the WHERE clause


SELECT
pg.PRODUCT_GROUP
, pg.DESCRIPTION
FROM PRODUCT_GROUPS pg
;
Revisiting the query we
PRODUCT_GROUP DESCRIPTION played with last week, it
412 Cloud_Software_Applications provides us with two columns
from the PRODUCT_GROUPS
437 Amazon_Points
table – and returns ALL the
414 A_Drive
rows in that table – 119 rows.
416 Deal_Sourcer
417 Amazon_Sourced
420 Financial_Products
424 Digital_Text_2
425 Digital_Accessories_2
251 Gourmet
241 Watches
SELECT
pg.PRODUCT_GROUP
, pg.DESCRIPTION
FROM PRODUCT_GROUPS pg
;
Most of the time, you only
PRODUCT_GROUP DESCRIPTION want a subset of the rows in a
412 Cloud_Software_Applications table. Some tables have over
437 Amazon_Points 1 Billion rows – so effective
414 A_Drive filtering is key.
416 Deal_Sourcer
417 Amazon_Sourced
420 Financial_Products
424 Digital_Text_2
425 Digital_Accessories_2
251 Gourmet
241 Watches
SELECT
pg.PRODUCT_GROUP
, pg.DESCRIPTION
FROM PRODUCT_GROUPS pg
WHERE
The main filtering method in
SQL is a WHERE clause, which
; comes after the FROM clause
(and before the ORDER BY
clause, if there is one).
SELECT
pg.PRODUCT_GROUP
, pg.DESCRIPTION
FROM PRODUCT_GROUPS pg
WHERE
pg.PRODUCT_GROUP = 14
;

To use it, we enter one or


more conditions that must be
true for the records to be
returned.
SELECT
pg.PRODUCT_GROUP
, pg.DESCRIPTION
FROM PRODUCT_GROUPS pg
WHERE
pg.PRODUCT_GROUP = 14
; Here we’re saying that the value in
the PRODUCT_GROUP column of the
table PRODUCT_GROUPS must be
equal to 14 for a row to be returned.
SELECT
pg.PRODUCT_GROUP
, pg.DESCRIPTION
FROM PRODUCT_GROUPS pg
WHERE
pg.PRODUCT_GROUP = 14
;

When Oracle runs the SQL


query, it checks every row in
the table to see if it’s TRUE
that the value in the
PRODUCT_GROUP column is
equal to 14.
SELECT
pg.PRODUCT_GROUP
, pg.DESCRIPTION
FROM PRODUCT_GROUPS pg
WHERE
pg.PRODUCT_GROUP = 14
And the results are a
; single row – the only
one in the table where
the PRODUCT_GROUP
PRODUCT_GROUP DESCRIPTION column was equal to
14.
14 Books
SELECT
pg.PRODUCT_GROUP
, pg.DESCRIPTION
FROM PRODUCT_GROUPS pg
WHERE
pg.PRODUCT_GROUP = 14
; The equals sign in this
condition is known as
the operator.
PRODUCT_GROUP DESCRIPTION
14 Books
SELECT
pg.PRODUCT_GROUP
, pg.DESCRIPTION
FROM PRODUCT_GROUPS pg The opposite of equals
WHERE (not equals) has it’s
own operator - !=
pg.PRODUCT_GROUP != 14
;
SELECT
pg.PRODUCT_GROUP
, pg.DESCRIPTION
FROM PRODUCT_GROUPS pg In fact, it has two. You
WHERE can also use <> as the
‘not equal to’ operator.
pg.PRODUCT_GROUP <> 14
;
SELECT
pg.PRODUCT_GROUP
, pg.DESCRIPTION
FROM PRODUCT_GROUPS pg
WHERE
pg.PRODUCT_GROUP <> 14
;
Both produce the
PRODUCT_GROUP DESCRIPTION
424 Digital_Text_2
same results – all
425 Digital_Accessories_2 records in the table
437 Amazon_Points where it is true that
421 Camera PRODUCT_GROUP is
422 Mobile_Electronics NOT equal to 14.
420 Financial_Products
251 Gourmet
241 Watches
236 Misc SDP
SELECT Another operator is IN –
which checks if a
pg.PRODUCT_GROUP column is equal to any
of the values in a list.
, pg.DESCRIPTION
FROM PRODUCT_GROUPS pg
WHERE
pg.PRODUCT_GROUP IN (14,15,64)
;
SELECT Another operator is IN –
which checks if a
pg.PRODUCT_GROUP column is equal to any
of the values in a list.
, pg.DESCRIPTION
FROM PRODUCT_GROUPS pg
WHERE
pg.PRODUCT_GROUP IN (14,15,64)
;
The list is enclosed in
parentheses, and the values
are separated by commas.
SELECT
pg.PRODUCT_GROUP
, pg.DESCRIPTION
FROM PRODUCT_GROUPS pg
WHERE
pg.PRODUCT_GROUP IN (14,15,64)
; This time, the results
include two rows – those
PRODUCT_GROUP DESCRIPTION matching any of the values
15 Music in the list.
14 Books Since there is no
PRODUCT_GROUP equal to
64 in the table, no record
returned for that value.
SELECT
pg.PRODUCT_GROUP
, pg.DESCRIPTION
FROM PRODUCT_GROUPS pg
WHERE
pg.PRODUCT_GROUP NOT IN (14,15,64)
; And it’s opposite is NOT IN.
SELECT
pg.PRODUCT_GROUP
, pg.DESCRIPTION
FROM PRODUCT_GROUPS pg
WHERE
pg.PRODUCT_GROUP < 14
;
You can also use greater
than and less than symbols
as operators…
PRODUCT_GROUP DESCRIPTION
-1 Mixed
0 Unassigned
SELECT
pg.PRODUCT_GROUP
, pg.DESCRIPTION
FROM PRODUCT_GROUPS pg
WHERE
pg.PRODUCT_GROUP <= 14
;
As well as greater than or
equal to, and less than or
equal to PRODUCT_GROUP DESCRIPTION
-1 Mixed
0 Unassigned
14 Books
SELECT BETWEEN is a handy
operator for ranges of
pg.PRODUCT_GROUP numbers, values or
(especially) dates.
, pg.DESCRIPTION
FROM PRODUCT_GROUPS pg
WHERE
pg.PRODUCT_GROUP BETWEEN 20 AND 27
;
PRODUCT_GROUP DESCRIPTION BETWEEN is inclusive
23 Electronics of both values listed,
21 Toys so we get results for
22 Games PRODUCT_GROUP 20,
20 Gifts PRODUCT_GROUP 27,
27 Video and everything in
26 Unknown between.
SELECT There’s also an operator
for finding blanks in a
pg.PRODUCT_GROUP column: IS NULL
, pg.DESCRIPTION
, pg.CREATION_DATE
FROM PRODUCT_GROUPS pg
WHERE pg.CREATION_DATE IS NULL
; PRODUCT_GROUP DESCRIPTION CREATION_DATE
23 Electronics
21 Toys
18 Unknown
22 Games
20 Gifts
15 Music
14 Books
-1 Mixed
27 Video
60 Home Improvement
0 Unassigned
SELECT And it’s opposite – IS NOT
NULL – which returns all
pg.PRODUCT_GROUP rows where a column is
, pg.DESCRIPTION not blank.

, pg.CREATION_DATE
FROM PRODUCT_GROUPS pg
WHERE pg.CREATION_DATE IS NOT NULL
; PRODUCT_GROUP DESCRIPTION CREATION_DATE
416 Deal_Sourcer 31-Mar-11
417 Amazon_Sourced 31-Mar-11
424 Digital_Text_2 15-Jul-11
425 Digital_Accessories_2 15-Jul-11
414 A_Drive 20-Oct-10
251 Gourmet 4-Aug-03
241 Watches 4-Jun-03
236 Misc SDP 20-Dec-02
234 Travel Store 9-Sep-02
259 Sports Memorabilia 31-Oct-03
SELECT Another handy operator is
LIKE. It allows you to
pg.PRODUCT_GROUP search for a value in a
column that contains a
, pg.DESCRIPTION certain text string.
, pg.SHORT_DESC
FROM PRODUCT_GROUPS pg
WHERE pg.SHORT_DESC LIKE '%To%'
; It’s combined with a wildcard that takes the place of the rest of
the string.
For example, here we’re using the
SELECT LIKE operator to look for any value in
the SHORT_DESC column that has
pg.PRODUCT_GROUP the letters ‘To’ in it. We use the %
wildcard to take the place of any
, pg.DESCRIPTION characters on either side of the To.

, pg.SHORT_DESC
FROM PRODUCT_GROUPS pg
WHERE pg.SHORT_DESC LIKE '%To%'
;
For example, here we’re using the
SELECT LIKE operator to look for any value in
the SHORT_DESC column that has
pg.PRODUCT_GROUP the letters ‘To’ in it. We use the %
wildcard to take the place of 0-many
, pg.DESCRIPTION characters on either side of the To.
, pg.SHORT_DESC
FROM PRODUCT_GROUPS pg
WHERE pg.SHORT_DESC LIKE '%To%'
; It all gets put in single
quotes, because it’s a
text string.
For example, here we’re using the
SELECT LIKE operator to look for any value in
the SHORT_DESC column that has
pg.PRODUCT_GROUP the letters ‘To’ in it. We use the %
wildcard to take the place of 0-many
, pg.DESCRIPTION characters on either side of the To.
, pg.SHORT_DESC
FROM PRODUCT_GROUPS pg
WHERE pg.SHORT_DESC LIKE '%To%'
; It all gets put in single
quotes, because it’s a
text string.

This would match to ‘Tools’, ‘Toys’, ‘Big Tops’, or


another other text string with the letters ‘To’.
SELECT
pg.PRODUCT_GROUP In our case, it matched
to ‘Toys’ and ‘Tools’
, pg.DESCRIPTION
, pg.SHORT_DESC
FROM PRODUCT_GROUPS pg
WHERE pg.SHORT_DESC LIKE '%To%'
;

PRODUCT_GROUP DESCRIPTION SHORT_DESC


21 Toys Toys
60 Home Improvement Tools
SELECT
pg.PRODUCT_GROUP Notice that it didn’t,
however, match to
, pg.DESCRIPTION ‘Auto’ or ‘Stones’, even
though those two
, pg.SHORT_DESC values exist in rows of
FROM PRODUCT_GROUPS pg this table.
WHERE pg.SHORT_DESC LIKE '%To%'
;
PRODUCT_GROUP DESCRIPTION SHORT_DESC
21 Toys Toys
60 Home Improvement Tools

PRODUCT_GROUP DESCRIPTION SHORT_DESC


263 Automotive Auto
246 Loose Stones Stones
SELECT It’s because anything in
single-quotes is CASE
pg.PRODUCT_GROUP sensitive in SQL, and
the T in ‘Auto’ and
, pg.DESCRIPTION ‘Stones’ is not
capitalized.
, pg.SHORT_DESC
FROM PRODUCT_GROUPS pg
WHERE pg.SHORT_DESC LIKE '%To%'
;
PRODUCT_GROUP DESCRIPTION SHORT_DESC
21 Toys Toys
60 Home Improvement Tools

PRODUCT_GROUP DESCRIPTION SHORT_DESC


263 Automotive Auto
246 Loose Stones Stones
SELECT
pg.PRODUCT_GROUP
, pg.DESCRIPTION
, pg.SHORT_DESC
FROM PRODUCT_GROUPS pg
WHERE UPPER(pg.SHORT_DESC) LIKE '%TO%'
;
One way around that case-sensitive issue it to wrap our column
name with the UPPER() function.

As Oracle evaluates each row in the table, it converts the value


in the SHORT_DESC column into all capital letters. As long we
use all capitals in our string, it will match ‘TO’, ‘to’, ‘To’, or ‘tO’.
SELECT
pg.PRODUCT_GROUP
, pg.DESCRIPTION
, pg.SHORT_DESC
FROM PRODUCT_GROUPS pg
WHERE UPPER(pg.SHORT_DESC) LIKE '%TO%'
;
PRODUCT_GROUP DESCRIPTION SHORT_DESC
21 Toys Toys
60 Home Improvement Tools
263 Automotive Auto
246 Loose Stones Stones
SELECT Probably not a surprise at this
point, but LIKE has an opposite,
pg.PRODUCT_GROUP too - NOT LIKE – that returns any
row where the value in the
, pg.DESCRIPTION specified field doesn’t include
, pg.SHORT_DESC the string.
FROM PRODUCT_GROUPS pg
WHERE UPPER(pg.SHORT_DESC) NOT LIKE '%TO%'
;
PRODUCT_GROUP DESCRIPTION SHORT_DESC
251 Gourmet Gourmt
241 Watches Watches
309 Shoes Shoes
23 Electronics CE
267 Musical Instruments Mus Instr
65 Software SW
15 Music Music
14 Books Books
27 Video Video
74 Video DVD DVD
SELECT Worth noting is that NOT LIKE,
NOT IN, and != do not return
pg.PRODUCT_GROUP nulls. For example, we get none
of the rows in the table below
, pg.DESCRIPTION where SHORT_DESC is null, even
, pg.SHORT_DESC though there are many.
FROM PRODUCT_GROUPS pg
WHERE UPPER(pg.SHORT_DESC) NOT LIKE '%TO%'
;
PRODUCT_GROUP DESCRIPTION SHORT_DESC
251 Gourmet Gourmt
241 Watches Watches
309 Shoes Shoes
23 Electronics CE
267 Musical Instruments Mus Instr
65 Software SW
15 Music Music
14 Books Books
27 Video Video
74 Video DVD DVD
A null value in a
column (aka a blank)
SELECT can’t be equal or not
pg.PRODUCT_GROUP equal to anything, so
must be handled via
, pg.DESCRIPTION IS NULL or IS NOT
, pg.SHORT_DESC NULL…

FROM PRODUCT_GROUPS pg
WHERE UPPER(pg.SHORT_DESC) IS NULL
;

PRODUCT_GROUP DESCRIPTION SHORT_DESC


416 Deal_Sourcer
417 Amazon_Sourced
421 Camera
422 Mobile_Electronics
437 Amazon_Points
236 Misc SDP
234 Travel Store
259 Sports Memorabilia
258 Posters
SELECT …Or, you can use the
pg.PRODUCT_GROUP NVL() function.
, pg.DESCRIPTION
, pg.SHORT_DESC
FROM PRODUCT_GROUPS pg
WHERE NVL(pg.SHORT_DESC, 'Unknown') != 'CE'
;

When Oracle evaluates each row of the table, it translates any


nulls found in the SHORT_DESC column into the text string
‘Unknown’. Then it evaluates that result for whether it’s not
equal to ‘CE’. Since ‘Unknown’ isn’t equal to ‘CE’, it will return all
the null rows, as well as non-null rows that aren’t equal to ‘CE’.
SELECT
pg.PRODUCT_GROUP
, pg.DESCRIPTION
, pg.SHORT_DESC
FROM PRODUCT_GROUPS pg
WHERE NVL(pg.SHORT_DESC, 'Unknown') != 'CE'
;
PRODUCT_GROUP DESCRIPTION SHORT_DESC
416 Deal_Sourcer
417 Amazon_Sourced
251 Gourmet Gourmt
241 Watches Watches
236 Misc SDP
234 Travel Store
259 Sports Memorabilia
258 Posters
309 Shoes Shoes
21 Toys Toys
Of course, you can
SELECT filter on multiple
pg.PRODUCT_GROUP
conditions, too.
, pg.DESCRIPTION
FROM PRODUCT_GROUPS pg
WHERE
pg.PRODUCT_GROUP BETWEEN 20 AND 27
AND pg.CREATION_DATE IS NOT NULL
;

If BOTH conditions must be


true, separate them with AND.
SELECT
pg.PRODUCT_GROUP
, pg.DESCRIPTION
FROM PRODUCT_GROUPS pg
WHERE
pg.PRODUCT_GROUP BETWEEN 20 AND 27
OR pg.DESCRIPTION <> 'Electronics'
;

If EITHER condition being


true is sufficient,
separate them with OR.
SELECT
pg.PRODUCT_GROUP
, pg.DESCRIPTION
FROM PRODUCT_GROUPS pg
WHERE
pg.PRODUCT_GROUP BETWEEN 20 AND 27
OR pg.DESCRIPTION <> 'Electronics'
;

This would return all records where


PRODUCT_GROUP is between 20 and 27,
PLUS any records where the
DESCRIPTION is not equal to ‘Electronics’
SELECT
pg.PRODUCT_GROUP
, pg.DESCRIPTION
, pg.SHORT_DESC
, pg.CREATION_DATE
FROM PRODUCT_GROUPS pg
WHERE
pg.PRODUCT_GROUP BETWEEN 20 AND 27
AND (pg.SHORT_DESC IS NULL
OR pg.CREATION_DATE IS NULL)
;

You can also use parentheses to control


how the conditions are applied
In this case, the PRODUCT_GROUP
SELECT must be between 20 and 27 AND
pg.PRODUCT_GROUP the EITHER the SHORT_DESC must
, pg.DESCRIPTION be null OR the CREATION_DATE
, pg.SHORT_DESC must be null.
, pg.CREATION_DATE
FROM PRODUCT_GROUPS pg
WHERE
pg.PRODUCT_GROUP BETWEEN 20 AND 27
AND (pg.SHORT_DESC IS NULL
OR pg.CREATION_DATE IS NULL)
;
PRODUCT_GROUP DESCRIPTION SHORT_DESC CREATION_DATE
23 Electronics CE
21 Toys Toys
22 Games
20 Gifts
27 Video Video
26 Unknown 9-Mar-00
SELECT
pg.PRODUCT_GROUP Without the parentheses, the
, pg.DESCRIPTION results could be very different.
, pg.SHORT_DESC
, pg.CREATION_DATE
FROM PRODUCT_GROUPS pg
WHERE
pg.PRODUCT_GROUP BETWEEN 20 AND 27
AND pg.SHORT_DESC IS NULL
OR pg.CREATION_DATE IS NULL
;
SELECT
pg.PRODUCT_GROUP Without the parentheses, the
, pg.DESCRIPTION results could be very different.
, pg.SHORT_DESC
, pg.CREATION_DATE
FROM PRODUCT_GROUPS pg
WHERE
pg.PRODUCT_GROUP BETWEEN 20 AND 27
AND pg.SHORT_DESC IS NULL
OR pg.CREATION_DATE IS NULL
;
Removing parentheses means the condition after the OR is sufficient
to return results, so any row where CREATION_DATE is null will be
returned, regardless of the other two conditions.
SELECT So rows where the
CREATION_DATE is null but
pg.PRODUCT_GROUP the PRODUCT_GROUP is NOT
, pg.DESCRIPTION between 20 and 27 are now
, pg.SHORT_DESC returned.
, pg.CREATION_DATE
FROM PRODUCT_GROUPS pg
WHERE
pg.PRODUCT_GROUP BETWEEN 20 AND 27
AND pg.SHORT_DESC IS NULL
OR pg.CREATION_DATE IS NULL
; PRODUCT_GROUP DESCRIPTION SHORT_DESC CREATION_DATE
23 Electronics CE
21 Toys Toys
18 Unknown
22 Games
20 Gifts
15 Music Music
14 Books Books
-1 Mixed
Explain Plan
BI Metadata
Troubleshooting
Altering Job Runs
Help

You might also like