📺 Develpreneur YouTube Episode

Video + transcript

SQL Tutorial Where Clause Part 1(AND/OR)

2022-08-02 •Youtube

Detailed Notes

1. Where clause part 1 - simple desc horses select distinct age from horses;

select name,breed,age,dt_created,dt_updated from horses where age=15 and dt_created [greater than] '2022-06-01'

select name,breed,age,dt_created,dt_updated from horses where age=15 and dt_created [greater than] year('2022-06-01 12:00:00');

select name,breed,age,dt_created,dt_updated from horses where age=15 and dt_created [greater than] '2022';

select name,breed,age,dt_created,dt_updated from horses where age=15 and month(dt_created) [greater than] month('2022-06-01');

select month(dt_created),month('2022-01-06') from horses where age=15 and dt_created [greater than] '2022';

select name,breed,age,dt_created,dt_updated from horses where age=15 and day(dt_created) [greater than] day('2022-05-28');

select name,breed,age,dt_created,dt_updated from horses where age=0 or age=16 and dt_created [greater than] '2022-04-18'

select name,breed,age,dt_created,dt_updated from horses WHERE (age=0 OR age=16) AND (dt_created [greater than] '2022-04-18' OR dt_updated[greater than] '2022-04-18');

Transcript Text
[Music]
well hello and welcome back we are
continuing our sql maria db
tutorials and we're continuing looking
at where clauses
now last time around we looked at uh
sort of some basic ones we did get into
ands and oars but now
i want to play around a little
differently with this one get a little
more complex so we're working with this
horse's table
and
let's say
um let's do let's do select distinct
weight
from horses
so we've got a whole bunch
um
so
looks like uh
let's do this
so now what we want to do
is
uh i don't want to do this i want to do
this
let's do this first
so now what we can do is we can do like
our normal select star
from horses
where
let's say
weight
is less than
uh 100.
so we're going to start with that simple
where clause
so there we've got this nice complicated
result set now what we can do we can
simplify it and we're going to say now
what we're going to do is we're going to
take all of those ids
and so now you're going to get this
now this is sort of a manufactured
example but
that's okay
so now i'm going to do this and what i
can do is in my where clause is i can
say select star from
horses
where uh let's do let's see what else we
want let's pick another field just cuz
um
oh let's do our ages
okay so let's put their age greater than
uh
greater than 10. okay
so we could do where age is greater than
10
and we're going to get a whole bunch of
rows back i'm sure if we do that so if
we do that there we got 1 000 but what
we can also do now we could just do
which is what we're normally going to do
so this little manufacturer but normally
we could do is we could say where age is
greater than 10
and
weight is less than 100.
and then we're sign we're set to go get
1241 rows but let's say that it wasn't
those are like a maybe it's a field in
another table or something like that
or we had to double join it so what we
could also do
is we could say oh let's just keep that
one there
and instead we're going to come down
here and we're going to say and
id in
this is like i said it's a little
backwards way to do it you would not
normally do this but you would
if this inner select or if this select
here was off of another table quite
possibly so now if i do
that and that took 0.433 seconds let's
see what it takes to do this a little
longer because it's doing some funky
stuff but we still got the same number
of rows back
and so when you looked at tables
did we have one
it's like we have uh owners
so we can take our owner's table
uh let's describe that
so we have an owner id and a sire and
then we have
in horses i think it's called an owner
id yep an owner id so we could do
select
star from
owner
where
owner id
in
so let's get all the owners of horses
this is not necessarily the only way you
could do it but
we could come in here and say let's go
oh that's right so now we're going to
select own rd from horses where weight
is less 100 and we're going to get the
owner information from them
uh oh it's owners
my mistake
oh semicolon and now we're getting the
owner information based on whether they
have a horse where the race where the
weight is less than 100. so we can
actually put
some
pretty complicated stuff into our where
clause we can actually sort of combine
now
you probably
for the most part do not want to do that
you want to use inner joins
but
there are times where
it makes sense to have a select in the
in the where clause um sometimes it's
actually
and we've i think we've actually looked
at this a little bit before so there's a
couple ways to break this down and this
is really getting a little bit in
performance tuning and stuff but that's
okay now what we ideally what we would
do is select star from well let's say uh
call owners o
so what we really want is we want to
select oh
that's not it's a zero no no oh dot star
from owners
inner join
uh horses
on
uh let's make that horses.h
on
h dot owner id equals o dot id
and we can actually do it here and we
can say and
h dot weight
less than 100.
and now we don't even have a where
clause at all
and we should get 356 rows back
whoops uh
oh dot id
enter join horses h on
oh it's owner id i'm sorry it's not id
it is owner id
so if we do that boom then we get our
356 rows back so now we're dealing with
an inner join
another way we can do this
is
we can say
let's do the o from owners again
and we can actually do we can take this
select
uh let's see like this and we're going
to call it uh
oq for the owner's query
on
oq dot
owner id equals h uh
yeah o dot owner id
oops i should spell it right
let me put some semicolons here to save
a few people if you're looking at these
in the notes
and so now i get the same thing now i
don't know if this went faster
uh that was 0.13 this is 0.08 so it
actually went faster this way
uh which is interesting because actually
what it did
is um
it builds this first it goes to that
inner piece it's going to build it first
and so it's got a much smaller amount to
uh to join to then he
uh
here oh where's the entire horses table
and it's
substantially different
because now this is joining to the horse
table and then comes in and handles the
that
you could also do
this and i can say where weight is less
than 100 where
if i do that let's see how fast that
comes back that's 0.103 so that's even
not as fast as the other one was
so you've got a couple of different ways
you can move around stuff
uh between interselects
or inner joins and selects and putting
those selects into your
um
into your where clause now for example
one of the things we might want to do is
let's say
select star
what we want to do is let's say we want
to get
all of the horses
where
age equals
minimum age
now if you try to do that because you
want it to be the minimum right so i'm
going to say i just want to give
everybody where it's the minimum age and
the table well it's not going to like
that because
you have to have a group by and we've
talked a little bit about those
so what you really want to do is you
need to say select
uh minimum
age
from horses
so if i do that and let's say
as age
so i can just do that
or i can do
select uh let's say
yeah that's probably going to want to do
i'm trying to think a little bit how i
do that
[Music]
yeah that's probably the easiest way to
do that i'm sorry i'm thinking a little
bit on the fly so now what i can do
and this would be a case where i'd want
to do it is i can say select
age comma weight
from horses where
age equals and so now
or actually it probably should be age
and i guess it doesn't matter
if i do that now i'm gonna see all of
these guys
with their uh
age and weight so that negative one so
let's say
uh max
and so here's a little more
typical kind of example is that what i'm
trying to do is
get a grouping together and there's most
of them
there now that's only one and so he
doesn't weigh anything even though his
age of 16.
so in this case i'm i'm doing some of my
uh my maxes my groups groupings and
things like that in that select
and then using that as part of the where
clause and particularly if you're using
mins and max that doesn't really
it's really not going to make sense to
do the inner join although it probably
could be done
i'm not sure how fast it's going to run
but from a
readability point of view
what you're going to see here is if
people look at it and they'll say oh i'm
going to select my values
from my table
like this
and then my where clause
is oh okay
where my age is select max age
and this sometimes a way to build a craw
if you're trying to sort of like join
across a or not join but
whittle down your results across a lot
of different tables sometimes it's
easier to get that join
that select in the where clause
as opposed to or i guess you can do an
interjoint with it as well but use those
queries to sort of get yourself to a
a smaller result set first
and then work from there which you could
do
you could do it in like a stored
procedure or a function or something
like that but
sometimes that doesn't
it doesn't lend itself to that as well
so you want you know maybe you've got it
you're for some reason restricted to a
query
so you can do it like this and you can
give yourself
sometimes a pretty complex query
but you can include uh with that some of
these uh you know some other selects and
some other tables
and you can even get complex with that
because you can actually have a i mean
you have a full-blown
um
so like if i did
if i do o dot id uh sorry that's
owner id
so i could take this
and that's going to give me this i can't
remember how many results this gives me
back okay that gives you 356 rows back
so what i could actually then turn
around and do is say
select star
or let's just do
uh age comma weight comma breed
from horses
where
our id
in
uh this big massive thing
watched it this way so it's just easier
to see
and i'm also gonna do
um let's say we're age less than 10.
and and i'm just
adding random stuff at this point but no
i've got like some
that this is getting pretty nasty i've
got a pretty gnarly
uh oh it's not h it's h comma
because i've got this really i'm
starting to get a pretty complex thing
in my where clause not recommended
uh sometimes those things are i don't
know if they're
you have to do it but sometimes you do
so now we're
we've been adjusting this a bit but
you're gonna see all your ages here
are gonna be over here in this left
column we're now less than 10.
so you can get
like i said you can get a little uh
interesting with your where clauses and
it's usually a matter of either the
energy uh the
using the n uh
here like where owner id in
but if it's a single number i can do
equals like where did i do
my match yeah so i can also do
from this one
the problem is you got to make sure what
your result set is so if i go where h
equals
because i only get one
then this will work
and it'll probably come back a little
faster than it did let's see 0.341
versus
oh that may have been cached a little
bit so
um but you can also get but so you can
do equals but if you try to do equals
where you're gonna get multiples so if i
said here
uh let me leave that there
i'm gonna break it
so now if i say he equals that
or she i guess doesn't matter what its
gender is i can come in and it's going
to say here that sub query returns more
than one row because i'm saying owner id
equals but this thing
this big ugly thing
returns lots of rows 356 rows so if
you've got if you if you know you're
only going to have one row then you're
okay
if you are going to have
some number of rows or you can okay with
equal if you're going to have a large
number of rows or actually more than one
row then go ahead and make it n
i n
and then
the challenge becomes what if it doesn't
exist
so
if i take
let me do one let's go back to a simpler
one
so let's do select from owners so select
owner id from horses where weight is
less than
uh negative one
so let's look at that first
this we don't get anything back so now
let's see what happens when we try to do
the owner id in
and so we're also going to get nothing
back
so in this case we don't get any rows
but
yes because we can't it's not in there
but we don't get an error or anything
like that it's just going to say oh
there's nothing left
it's still going to run all this
so technically
yeah you may want to do like in those
cases you're probably better off to do
an inner join
um and of course
if you start messing around the where
clause then
it's sometimes gonna be easier to join
and then do like an inner or left join
as opposed to
um some sort of you know equals or in in
your query
i think we've gone far enough into our
where clauses this time and so
we'll come back we'll explore some other
fun stuff around these as
interesting stories come up and
interesting approaches come up to do
some of our queries
and some of our
tuning and things of that nature but for
now we'll let you get back to it so go
out there and have yourself a great day
a great week and we will talk to you
next time
you
Transcript Segments
0.43

[Music]

27.279

well hello and welcome back we are

30

continuing our sql maria db

33.84

tutorials and we're continuing looking

36

at where clauses

37.6

now last time around we looked at uh

40.399

sort of some basic ones we did get into

42.48

ands and oars but now

45.039

i want to play around a little

46.96

differently with this one get a little

48.48

more complex so we're working with this

50.559

horse's table

52.079

and

53.28

let's say

55.28

um let's do let's do select distinct

59.28

weight

60.8

from horses

64

so we've got a whole bunch

66.24

um

67.68

so

68.56

looks like uh

70.4

let's do this

73.439

so now what we want to do

76.24

is

77.759

uh i don't want to do this i want to do

79.2

this

79.92

let's do this first

83.759

so now what we can do is we can do like

87.2

our normal select star

89.439

from horses

92

where

96

let's say

97.2

weight

100.4

is less than

103.36

uh 100.

105.119

so we're going to start with that simple

106.56

where clause

111.84

so there we've got this nice complicated

114.799

result set now what we can do we can

117.52

simplify it and we're going to say now

119.52

what we're going to do is we're going to

120.719

take all of those ids

123.119

and so now you're going to get this

126.399

now this is sort of a manufactured

128.16

example but

129.52

that's okay

131.92

so now i'm going to do this and what i

133.84

can do is in my where clause is i can

136.959

say select star from

139.76

horses

140.84

where uh let's do let's see what else we

144.48

want let's pick another field just cuz

146.879

um

149.52

oh let's do our ages

160.08

okay so let's put their age greater than

163.2

uh

164.4

greater than 10. okay

167.04

so we could do where age is greater than

170.16

10

172.879

and we're going to get a whole bunch of

174.56

rows back i'm sure if we do that so if

176.319

we do that there we got 1 000 but what

179.44

we can also do now we could just do

182

which is what we're normally going to do

183.28

so this little manufacturer but normally

185.44

we could do is we could say where age is

187.44

greater than 10

189.12

and

191.04

weight is less than 100.

195.44

and then we're sign we're set to go get

198.76

1241 rows but let's say that it wasn't

203.2

those are like a maybe it's a field in

204.959

another table or something like that

208.08

or we had to double join it so what we

210.319

could also do

212.799

is we could say oh let's just keep that

214.879

one there

217.28

and instead we're going to come down

218.72

here and we're going to say and

222.4

id in

227.68

this is like i said it's a little

228.799

backwards way to do it you would not

230

normally do this but you would

232.239

if this inner select or if this select

234.64

here was off of another table quite

236.4

possibly so now if i do

238.84

that and that took 0.433 seconds let's

242.08

see what it takes to do this a little

243.76

longer because it's doing some funky

245.36

stuff but we still got the same number

246.959

of rows back

249.28

and so when you looked at tables

254.56

did we have one

256.88

it's like we have uh owners

262.479

so we can take our owner's table

264.72

uh let's describe that

268.8

so we have an owner id and a sire and

270.88

then we have

273.919

in horses i think it's called an owner

275.68

id yep an owner id so we could do

279.759

select

281.04

star from

282.88

owner

286.639

where

290.479

owner id

292.479

in

295.6

so let's get all the owners of horses

297.6

this is not necessarily the only way you

298.8

could do it but

304.08

we could come in here and say let's go

307.68

oh that's right so now we're going to

309.199

select own rd from horses where weight

311.44

is less 100 and we're going to get the

313.12

owner information from them

320.24

uh oh it's owners

322.32

my mistake

330.56

oh semicolon and now we're getting the

333.36

owner information based on whether they

335.6

have a horse where the race where the

337.12

weight is less than 100. so we can

339.68

actually put

341.36

some

342.96

pretty complicated stuff into our where

345.68

clause we can actually sort of combine

347.84

now

348.96

you probably

350

for the most part do not want to do that

352.08

you want to use inner joins

354.16

but

355.6

there are times where

357.68

it makes sense to have a select in the

360.56

in the where clause um sometimes it's

363.52

actually

364.4

and we've i think we've actually looked

365.84

at this a little bit before so there's a

367.52

couple ways to break this down and this

369.199

is really getting a little bit in

370.8

performance tuning and stuff but that's

372.16

okay now what we ideally what we would

374.4

do is select star from well let's say uh

377.12

call owners o

379.12

so what we really want is we want to

380.639

select oh

382

that's not it's a zero no no oh dot star

384.72

from owners

385.919

inner join

388.319

uh horses

390.56

on

393.12

uh let's make that horses.h

396.88

on

398.56

h dot owner id equals o dot id

406.96

and we can actually do it here and we

408.16

can say and

409.68

h dot weight

412.479

less than 100.

414

and now we don't even have a where

415.12

clause at all

417.84

and we should get 356 rows back

420.96

whoops uh

422.88

oh dot id

429.599

enter join horses h on

439.12

oh it's owner id i'm sorry it's not id

442

it is owner id

447.68

so if we do that boom then we get our

449.84

356 rows back so now we're dealing with

451.759

an inner join

453.52

another way we can do this

456.08

is

457.36

we can say

458.96

let's do the o from owners again

465.599

and we can actually do we can take this

467.919

select

472.8

uh let's see like this and we're going

474.96

to call it uh

476.879

oq for the owner's query

480.16

on

481.96

oq dot

484.16

owner id equals h uh

489.12

yeah o dot owner id

493.28

oops i should spell it right

498.08

let me put some semicolons here to save

499.919

a few people if you're looking at these

502.4

in the notes

503.84

and so now i get the same thing now i

506

don't know if this went faster

512.8

uh that was 0.13 this is 0.08 so it

516.159

actually went faster this way

519.039

uh which is interesting because actually

520.479

what it did

522

is um

524.08

it builds this first it goes to that

525.68

inner piece it's going to build it first

527.92

and so it's got a much smaller amount to

531.519

uh to join to then he

534.72

uh

536.88

here oh where's the entire horses table

539.44

and it's

540.399

substantially different

542.56

because now this is joining to the horse

544.48

table and then comes in and handles the

547.839

that

548.959

you could also do

552.48

this and i can say where weight is less

555.6

than 100 where

561.2

if i do that let's see how fast that

562.56

comes back that's 0.103 so that's even

564.88

not as fast as the other one was

568.32

so you've got a couple of different ways

570

you can move around stuff

572.24

uh between interselects

574.399

or inner joins and selects and putting

578.32

those selects into your

580.72

um

582.16

into your where clause now for example

585.44

one of the things we might want to do is

587.92

let's say

589.519

select star

591.12

what we want to do is let's say we want

592.48

to get

593.36

all of the horses

595.279

where

597.6

age equals

599.36

minimum age

601.76

now if you try to do that because you

603.6

want it to be the minimum right so i'm

605.6

going to say i just want to give

606.56

everybody where it's the minimum age and

608.24

the table well it's not going to like

610

that because

611.519

you have to have a group by and we've

613.519

talked a little bit about those

616.079

so what you really want to do is you

617.519

need to say select

620.16

uh minimum

624.16

age

626.56

from horses

629.839

so if i do that and let's say

634.399

as age

640.079

so i can just do that

644.839

or i can do

648.88

select uh let's say

655.2

yeah that's probably going to want to do

658.8

i'm trying to think a little bit how i

660.079

do that

662.25

[Music]

671.36

yeah that's probably the easiest way to

672.72

do that i'm sorry i'm thinking a little

673.839

bit on the fly so now what i can do

676.959

and this would be a case where i'd want

678

to do it is i can say select

680.64

age comma weight

688.32

from horses where

691.839

age equals and so now

696.32

or actually it probably should be age

698.24

and i guess it doesn't matter

703.839

if i do that now i'm gonna see all of

705.839

these guys

707.36

with their uh

709.76

age and weight so that negative one so

711.839

let's say

713.04

uh max

716.639

and so here's a little more

718.32

typical kind of example is that what i'm

720.24

trying to do is

722.32

get a grouping together and there's most

724.8

of them

725.839

there now that's only one and so he

727.44

doesn't weigh anything even though his

728.8

age of 16.

731.2

so in this case i'm i'm doing some of my

734.959

uh my maxes my groups groupings and

737.76

things like that in that select

741.68

and then using that as part of the where

743.44

clause and particularly if you're using

744.72

mins and max that doesn't really

748.399

it's really not going to make sense to

750

do the inner join although it probably

751.839

could be done

752.959

i'm not sure how fast it's going to run

755.12

but from a

756.8

readability point of view

759.76

what you're going to see here is if

761.44

people look at it and they'll say oh i'm

762.639

going to select my values

764.639

from my table

766.32

like this

768

and then my where clause

770.16

is oh okay

773.12

where my age is select max age

776

and this sometimes a way to build a craw

778.32

if you're trying to sort of like join

780.079

across a or not join but

782.88

whittle down your results across a lot

785.279

of different tables sometimes it's

786.88

easier to get that join

788.8

that select in the where clause

791.76

as opposed to or i guess you can do an

793.76

interjoint with it as well but use those

795.76

queries to sort of get yourself to a

798.639

a smaller result set first

801.839

and then work from there which you could

803.92

do

805.519

you could do it in like a stored

806.72

procedure or a function or something

808.56

like that but

810

sometimes that doesn't

811.6

it doesn't lend itself to that as well

814.399

so you want you know maybe you've got it

815.76

you're for some reason restricted to a

817.279

query

818.959

so you can do it like this and you can

820.16

give yourself

821.6

sometimes a pretty complex query

824.48

but you can include uh with that some of

826.88

these uh you know some other selects and

829.68

some other tables

831.199

and you can even get complex with that

833.36

because you can actually have a i mean

834.639

you have a full-blown

836.8

um

838.56

so like if i did

842

if i do o dot id uh sorry that's

845.839

owner id

847.68

so i could take this

851.519

and that's going to give me this i can't

852.88

remember how many results this gives me

854.16

back okay that gives you 356 rows back

857.36

so what i could actually then turn

858.72

around and do is say

861.519

select star

863.12

or let's just do

864.639

uh age comma weight comma breed

869.04

from horses

873.36

where

876.48

our id

879.12

in

881.839

uh this big massive thing

886.16

watched it this way so it's just easier

887.68

to see

890.72

and i'm also gonna do

892.72

um let's say we're age less than 10.

899.839

and and i'm just

901.279

adding random stuff at this point but no

903.12

i've got like some

904.399

that this is getting pretty nasty i've

906.32

got a pretty gnarly

908.079

uh oh it's not h it's h comma

912.8

because i've got this really i'm

914.16

starting to get a pretty complex thing

915.44

in my where clause not recommended

917.76

uh sometimes those things are i don't

919.92

know if they're

920.959

you have to do it but sometimes you do

922.72

so now we're

924.32

we've been adjusting this a bit but

925.76

you're gonna see all your ages here

928

are gonna be over here in this left

929.759

column we're now less than 10.

932.56

so you can get

934.16

like i said you can get a little uh

937.279

interesting with your where clauses and

940.32

it's usually a matter of either the

942.24

energy uh the

944.399

using the n uh

946.8

here like where owner id in

950.16

but if it's a single number i can do

952.72

equals like where did i do

954.639

my match yeah so i can also do

957.839

from this one

959.04

the problem is you got to make sure what

960.24

your result set is so if i go where h

962.48

equals

964.32

because i only get one

966.8

then this will work

969.519

and it'll probably come back a little

970.56

faster than it did let's see 0.341

973.04

versus

980

oh that may have been cached a little

981.36

bit so

982.639

um but you can also get but so you can

984.8

do equals but if you try to do equals

987.36

where you're gonna get multiples so if i

988.959

said here

990.639

uh let me leave that there

992.959

i'm gonna break it

996.16

so now if i say he equals that

998.8

or she i guess doesn't matter what its

1000.16

gender is i can come in and it's going

1002.32

to say here that sub query returns more

1004.399

than one row because i'm saying owner id

1007.36

equals but this thing

1010.72

this big ugly thing

1014.079

returns lots of rows 356 rows so if

1017.759

you've got if you if you know you're

1019.68

only going to have one row then you're

1021.519

okay

1022.639

if you are going to have

1025.12

some number of rows or you can okay with

1027.199

equal if you're going to have a large

1028.48

number of rows or actually more than one

1030.319

row then go ahead and make it n

1033.839

i n

1034.959

and then

1036.079

the challenge becomes what if it doesn't

1038.799

exist

1041.52

so

1043.039

if i take

1045.679

let me do one let's go back to a simpler

1047.52

one

1050.88

so let's do select from owners so select

1053.679

owner id from horses where weight is

1055.679

less than

1057.919

uh negative one

1060.08

so let's look at that first

1064.96

this we don't get anything back so now

1066.88

let's see what happens when we try to do

1068.48

the owner id in

1072.08

and so we're also going to get nothing

1073.44

back

1074.799

so in this case we don't get any rows

1076.88

but

1077.84

yes because we can't it's not in there

1080.48

but we don't get an error or anything

1081.84

like that it's just going to say oh

1083.2

there's nothing left

1085.28

it's still going to run all this

1087.039

so technically

1088.799

yeah you may want to do like in those

1090.4

cases you're probably better off to do

1091.52

an inner join

1092.84

um and of course

1095.52

if you start messing around the where

1096.88

clause then

1098.559

it's sometimes gonna be easier to join

1100.4

and then do like an inner or left join

1102.88

as opposed to

1104.88

um some sort of you know equals or in in

1107.36

your query

1109.039

i think we've gone far enough into our

1110.72

where clauses this time and so

1113.44

we'll come back we'll explore some other

1114.88

fun stuff around these as

1117.36

interesting stories come up and

1118.799

interesting approaches come up to do

1120.4

some of our queries

1121.679

and some of our

1123.52

tuning and things of that nature but for

1125.52

now we'll let you get back to it so go

1128.16

out there and have yourself a great day

1130.08

a great week and we will talk to you

1133.28

next time

1149.84

you