📺 Develpreneur YouTube Episode

Video + transcript

SQL Tutorial - Where Part 2 (where value in)

2022-08-04 •Youtube

Detailed Notes

1. Where clause part 2 - inner and sum

select * from horses where weight [less than] 100;

select * from horses where age [greater than] 10 and weight [less than] 100; select id from horses where weight [less than] 100;

select * from horses where age [greater than] 10 and id in (select id from horses where weight [less than] 100);

select * from owners where owner_id in (select owner_id from horses where weight [less than] 100);

select o.* from owners o inner join horses h on h.owner_id = o.owner_id and h.weight [less than] 100;

select o.owner_id from owners o inner join (select owner_id from horses where weight [less than] 100) oq on oq.owner_id=o.owner_id;

select o.* from owners o inner join horses h on h.owner_id = o.owner_id where h.weight [less than] 100;

select * from horses where age = min(age);

select * from owners where owner_id in (select owner_id from horses where weight [less than] -1);

select age,weight from horses where age in (select max(age) as age from horses);

select age,weight,breed from horses where age [less than] 10 and owner_id in (select o.owner_id from owners o inner join (select owner_id from horses where weight [less than] 100) oq on oq.owner_id=o.owner_id);

select age,weight from horses where age in (select max(age) as age from horses);

select age,weight,breed from horses where age [less than]10 and owner_id = (select o.owner_id from owners o inner join (select owner_id from horses where weight [less than] 100) oq on oq.owner_id=o.owner_id);

Transcript Text
[Music]
hello and welcome back
we are continuing our
our sequel
story of
lessons
uh getting into all kinds of little
things playing around with my sec my
sequel mysql and mariadb
this time we're going to get back to
some
some where clause related stuff
so we're going to play around with this
and probably the next episode as well so
we're going to start with some simpler
ones some of which we've seen but to go
a little more into those and then
um
get into some more you know some more
complex things so we're going to start
with just we'll go back to that horses
table because it's a nice big one and
it's got a bunch of
funky little fields
so since we've got it it's easy to look
at
now um what i did here is went ahead and
did a um
i've gotta describe so i just did
describe horses
just to see what
wow that just totally blew up on me let
me try that again
i hadn't seen that before
there we go
i don't know what happened there but
okay
so we've got a bunch of different fields
over here on the left
and typically what you're going to do is
you're going to do a select as we've
talked about and you can use where's in
other places we're going to start with
this so you could do like select star
from horses
and then you're gonna do wear something
so first
let's do this
let's figure out some of what we got so
let's do a distinct
distinct
age
from horses so let's see how old these
guys are and gals i guess
a whole bunch of different ones so we've
got some nulls we've got some negatives
in there
and then a couple of you know small
numbers we've got a zero we got a whole
bunch of different stuff
so let's take a look at
uh let's select star from well let's
just do
uh what do we want to call it
name so let's do
name
comma breed
comma age from horses and this is going
to be a simple one and we just do
where and we take a value so age equals
16. let's do that
so there's only one
now let's do let's see what do we have
with age 15
okay there's a munch there
now if we look at
let's get a little more complicated so
let's look at the
uh let's look at the date created
for the actu let's add date created and
date updated because i want to play
around some dates
so we're going to go here
and we're going to do uh dt created
comma dt updated
we haven't played a lot with dates but
we can see here
so we've got created on a whole bunch of
different days
and updated on
you know different days as well not only
yeah look like here they're not always
the same
now if we want to do
let's say so these looks like they
started on essentially april 15th
it was the earliest it was created and
june 7th was the latest so what we can
do
is instead of that we can do well well
actually let's do
out of this group
so now we're going to have we're going
to add so we're going to say and
and let me go ahead and do it this way
so i can keep it
make sure it's in our notes
so now what we're going to do and let's
do where dt created
is greater than let's say june 1st so
what we're going to do is we're going to
202 2-06-01
and if we take that
let's do this and
did i get that
and let's try that did it come through
okay yes it did okay
now notice here
that
six one has at 0 8 12 17. so this is
greater than 6 1 it's assuming because
we didn't put anything in there the time
is zero zero zero zero zero so
it is the same because it's just gonna
take what we got
so it's the same as zero zero zero zero
zero zero
we're gonna get those same three
but if we wanted to change that to say
12 o'clock
now we're only going to get two
and so we're
with dates notice i didn't have to do
anything i didn't have to do any
conversions or anything like that it's
basically going to take
um
it's going to take that
frame that date
as a more or less as a string and it's
going to do some conversion for us so we
don't have to do some complex stuff or
anything like that
now we could do
uh we've seen some of those so we could
do
um
we could do like year
and then we're going to see is we're
preparing to get none of these whoops
if i put that right
oh there we go um
and date created
oh it's just greater than the year so
i'm sorry so now
let me copy this one and take this
over here
i don't know what that is let's do this
there we go okay
so here
what it did is
it's taking just the year part
so it's anything where it's greater than
2022 so it's going to be anything that's
january 1st
if i wanted to do greater than
month
then i'm gonna find anything basically
you know june or later
so if i do that now i can see
[Music]
oh interesting so it
did not pick that up so i picked that up
wrong
i'm gonna try that one more time created
okay
yep
no interesting it didn't like the month
and i may be missing something if i do
day
well that's not gonna help me much
so
oh i wonder if that's reversed
so it's
is it did it take a reverse
nope it still didn't so a month it
doesn't like but year
well and that's right because it was
like a
i forget the conversion but
if we want to go back it's actually it's
not month this year gives us year but
month does not give us that because if
we do that
if i go here
and i just do select month
um
what's that 2022-oops
dash
06-01
oh it is so it should oh i'm sorry
so it's not i'd have to that's my
mistake i have to go
month here and
month here
with the year it was okay because it was
basically just looking at
let me show you let's see so if we do
this
uh
interesting it doesn't like that
so let's go look at this real quick
so first let me do this
let me go back to this little discussion
so when we did year that year
would be the same as doing this select
where it's greater than
2022
because it's basically like a string and
since 202 the year starts it
um
there so it's going to take everything
from that year now here
we've got this month and this month so
let's go look at this
select
because that doesn't seem so if we do
that
let's go look at these two
oh
oh
um let's do
uh from horses
let's just do that
because we've got to give it a from so
we can pick that dt created
and we'll just keep it simple
and so now we're going to see
month but here oh i'm sorry because i
reversed my months here
so
i didn't write that that was january i'm
sorry corrected that
i did not correct that
so now if i do month greater than june
if i get my stuff right
then i see
that's the wrong oh
sorry
let's do the actual query
and
so when the month is greater than so
it's gonna be greater than six but if i
go back and do may 1st it's going to
give me you know because it's just the
month it really doesn't matter what day
whether i do may 1st
it's going to give me all
junes if i do may
28th it doesn't matter it gives me the
same thing because all it cares about
is the month part and if i did
day on those two
i think it's day and maybe date
we'll find out in a second yep it's
so here like since this is 5 28
the date's only going to be if it's a 29
30 or i guess a 31 if you were to have
one
so we're able to do some monkeying
around with our dates to do that if we
go back and look here and notice that
we've got we've been using our ands so
now
let's go back and do uh select distinct
age from horses
and let's do
so now let's come back here
uh
and we're going to take this same thing
and now let me just do this
what we want to do so let's say just age
is 15 or let's start with this
or
h equals 16. or let's say
well let's do that first
so if we do this
um
oops gotta do that
then we're gonna see
there's an h15 and then there's h16s now
there was a zero and let's try that
negative one so let's go back and say we
want to hit age 16 or
age negative 1 because
those are the two high ends
and let's just go that way now we could
say
you know greater than 14 for the other
one and we get 15 and 16. so let's try
these two
so if we do these we see that oh we get
a bunch of those
there's a bunch of negative ones let's
do
zero i didn't even count these so let's
see what we got here
that's not too bad okay so there's only
a few zeros so
i'll clean this up a little bit just
because
so we can do these two and we can get
our
zero
uh we can get our ages 16 and zero now
let's put this here
let's say we also want to do
and date created as greater than
pardon my throat there my clearing is
greater than 61 so that's going to be so
let's say that's 9 so let's do
greater than
4 18.
and what we're going to see
so let's look at this one
let's see what it gives us
let's do that
so now if we do it so now we say where
the age equals 0 or h equals 16 and date
created equals
is greater than 418
then we ended up getting
all of them
because
we have this or and we don't really have
it
logically set up because it can be
either the age of 0 or
the age of 16
and the and basically just gets sort of
lost because we've already hit one of
the ores
so instead what we need to do
is take that same one
and if we wrap it in parentheses
then we're gonna get this so now it's
going to be either the age is equal to 0
or 16 and the date's created equal to
that
did i do that right yeah i did that
right okay
is that date created yeah they created
so now
i only get those two
because it's saying it's either one of
these two ages
and
because it combines says with those two
then it also has to be tt created
so you need to be aware of parentheses
when you're dealing with complex where
clauses now sometimes people will
actually write these
in a way to make it a little easier to
read is they'll do some formatting be
something like you select the columns
you do from
and then what you're going to do is
you're going to have
each of the clause type pieces
and they may even do it like this so
it's a little easier for you to sort of
read that
now another thing that you could do is
which is not super uncommon i've seen
done is where you'll do like
a clause and another clause
so i could also say if i do and
let's say date updated
is greater than
518. so now if i take this
so now i'm going to say i want only
where the date updated is that so i'm
going to see that so now i've got that
those two ores gathered but then i've
got an and another and so i've got three
main things for the
query
and now i get that
but if it's an or
then i'm going to get something really
funky
and i would have to actually i'm not
sure exactly how to
plot that one out but because it's an or
there you go it's because
um
we have this or updated or date updated
that thing just like so these two get
basically combined and this one says
oh or if dt update is grown that which
means everything over here that i'm
getting is going to have a date uh a dt
updated greater than 518 or if it
doesn't
then
it's got to be one of these other things
then it's you know then it this is why
it's hitting it because the age 0 or the
age of 16 and the date created is that
so again
if i wanted to combine these two
then i would have to do parentheses
around it and now i'll get
maybe nothing
oh no i do because both have a
oh where the date created is greater
than that or
the date updated is greater than that
but so in either case i'm in pretty good
shape
um
i can probably get a date updated late
greater than 14. i don't know if i'll
get another one let's try that real
quick see if a different record shows up
i haven't looked close enough quietly at
my
data nope get the same thing
i think that's a good point to stop for
this one
we're going to get into a little bit
more complicated stuff but really this
this episode this lesson i want to just
focus on
just wanted to focus on ands and ors and
making sure that you understand the
parentheses matter
so it's much less like logic you would
see in any other programming language
you know where there is some sort of
boolean logic of ands and ors and
equals and not equals and greater than
less than and those sorts of things
we've seen them a little bit
and now i just want to get a little
deeper next episode we will continue
digging into where clauses
but as always go out there and have
yourself a great day a great week and we
will talk to you
next time
[Music]
you
Transcript Segments
0.43

[Music]

26.88

hello and welcome back

28.56

we are continuing our

30.72

our sequel

32.16

story of

33.76

lessons

34.96

uh getting into all kinds of little

36.48

things playing around with my sec my

38.239

sequel mysql and mariadb

41.76

this time we're going to get back to

44.32

some

45.76

some where clause related stuff

48.8

so we're going to play around with this

50

and probably the next episode as well so

51.6

we're going to start with some simpler

52.879

ones some of which we've seen but to go

55.28

a little more into those and then

57.44

um

58.399

get into some more you know some more

60

complex things so we're going to start

61.84

with just we'll go back to that horses

63.44

table because it's a nice big one and

64.879

it's got a bunch of

66.32

funky little fields

68.24

so since we've got it it's easy to look

70.08

at

70.88

now um what i did here is went ahead and

74.64

did a um

77.439

i've gotta describe so i just did

78.88

describe horses

80.96

just to see what

84.56

wow that just totally blew up on me let

86.64

me try that again

88.88

i hadn't seen that before

94.96

there we go

97.68

i don't know what happened there but

99.68

okay

101.28

so we've got a bunch of different fields

102.96

over here on the left

104.24

and typically what you're going to do is

106

you're going to do a select as we've

108.799

talked about and you can use where's in

110.399

other places we're going to start with

111.439

this so you could do like select star

113.84

from horses

116.079

and then you're gonna do wear something

117.92

so first

119.2

let's do this

121.6

let's figure out some of what we got so

123.36

let's do a distinct

126.32

distinct

127.759

age

128.72

from horses so let's see how old these

130.56

guys are and gals i guess

133.92

a whole bunch of different ones so we've

135.36

got some nulls we've got some negatives

137.12

in there

138.08

and then a couple of you know small

139.68

numbers we've got a zero we got a whole

141.2

bunch of different stuff

142.959

so let's take a look at

145.92

uh let's select star from well let's

148.319

just do

150.48

uh what do we want to call it

152.72

name so let's do

155.36

name

158.319

comma breed

160.879

comma age from horses and this is going

163.12

to be a simple one and we just do

165.04

where and we take a value so age equals

168.239

16. let's do that

171.2

so there's only one

173.04

now let's do let's see what do we have

174.72

with age 15

178.159

okay there's a munch there

180.319

now if we look at

182.8

let's get a little more complicated so

184.48

let's look at the

186.959

uh let's look at the date created

191.28

for the actu let's add date created and

193.28

date updated because i want to play

194.48

around some dates

195.92

so we're going to go here

198.72

and we're going to do uh dt created

201.519

comma dt updated

204.239

we haven't played a lot with dates but

206.08

we can see here

208.159

so we've got created on a whole bunch of

210.239

different days

211.36

and updated on

213.28

you know different days as well not only

216.48

yeah look like here they're not always

218.4

the same

220.08

now if we want to do

222.239

let's say so these looks like they

223.599

started on essentially april 15th

226.48

it was the earliest it was created and

228.959

june 7th was the latest so what we can

231.36

do

233.68

is instead of that we can do well well

236.56

actually let's do

239.2

out of this group

241.12

so now we're going to have we're going

242.799

to add so we're going to say and

245.28

and let me go ahead and do it this way

247.76

so i can keep it

252

make sure it's in our notes

254.72

so now what we're going to do and let's

256.639

do where dt created

260

is greater than let's say june 1st so

263.68

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

264.56

202 2-06-01

270.96

and if we take that

274.479

let's do this and

286.72

did i get that

291.84

and let's try that did it come through

293.68

okay yes it did okay

296.08

now notice here

298

that

299.68

six one has at 0 8 12 17. so this is

304.32

greater than 6 1 it's assuming because

306.32

we didn't put anything in there the time

309.039

is zero zero zero zero zero so

312.32

it is the same because it's just gonna

314.16

take what we got

315.44

so it's the same as zero zero zero zero

318.32

zero zero

320.24

we're gonna get those same three

322.88

but if we wanted to change that to say

324.479

12 o'clock

326.72

now we're only going to get two

328.96

and so we're

330.24

with dates notice i didn't have to do

332.88

anything i didn't have to do any

334.639

conversions or anything like that it's

336.56

basically going to take

338.72

um

339.759

it's going to take that

341.28

frame that date

342.96

as a more or less as a string and it's

344.72

going to do some conversion for us so we

346.56

don't have to do some complex stuff or

348.72

anything like that

350

now we could do

351.44

uh we've seen some of those so we could

353.44

do

354.479

um

357.199

we could do like year

361.84

and then we're going to see is we're

362.72

preparing to get none of these whoops

365.28

if i put that right

368.4

oh there we go um

370.319

and date created

373.6

oh it's just greater than the year so

375.28

i'm sorry so now

376.88

let me copy this one and take this

381.68

over here

386.319

i don't know what that is let's do this

388.319

there we go okay

391.759

so here

392.88

what it did is

394.639

it's taking just the year part

398

so it's anything where it's greater than

399.919

2022 so it's going to be anything that's

401.919

january 1st

403.52

if i wanted to do greater than

406.319

month

409.039

then i'm gonna find anything basically

411.44

you know june or later

413.12

so if i do that now i can see

417.06

[Music]

420.24

oh interesting so it

423.84

did not pick that up so i picked that up

426.08

wrong

434.16

i'm gonna try that one more time created

437.599

okay

440.08

yep

441.36

no interesting it didn't like the month

444.96

and i may be missing something if i do

446.8

day

447.919

well that's not gonna help me much

451.44

so

452.319

oh i wonder if that's reversed

454.479

so it's

455.919

is it did it take a reverse

461.44

nope it still didn't so a month it

462.96

doesn't like but year

464.8

well and that's right because it was

466.08

like a

467.44

i forget the conversion but

469.84

if we want to go back it's actually it's

471.12

not month this year gives us year but

472.72

month does not give us that because if

474.4

we do that

476

if i go here

479.039

and i just do select month

483.199

um

484.8

what's that 2022-oops

489.36

dash

490.479

06-01

495.199

oh it is so it should oh i'm sorry

499.36

so it's not i'd have to that's my

501.599

mistake i have to go

503.84

month here and

506.879

month here

508.319

with the year it was okay because it was

510.56

basically just looking at

513.12

let me show you let's see so if we do

514.88

this

518.839

uh

520.8

interesting it doesn't like that

524.159

so let's go look at this real quick

526.64

so first let me do this

529.2

let me go back to this little discussion

532

so when we did year that year

535.2

would be the same as doing this select

540.16

where it's greater than

543.2

2022

545.36

because it's basically like a string and

547.12

since 202 the year starts it

550.24

um

552.16

there so it's going to take everything

554.32

from that year now here

557.519

we've got this month and this month so

559.6

let's go look at this

561.519

select

563.36

because that doesn't seem so if we do

565.2

that

569.279

let's go look at these two

571.44

oh

573.839

oh

574.839

um let's do

579.36

uh from horses

581.6

let's just do that

583.44

because we've got to give it a from so

585.04

we can pick that dt created

587.279

and we'll just keep it simple

593.04

and so now we're going to see

595.04

month but here oh i'm sorry because i

597.68

reversed my months here

600

so

601.6

i didn't write that that was january i'm

603.36

sorry corrected that

604.959

i did not correct that

607.04

so now if i do month greater than june

609.36

if i get my stuff right

612.56

then i see

613.68

that's the wrong oh

615.92

sorry

616.959

let's do the actual query

618.64

and

621.2

so when the month is greater than so

622.88

it's gonna be greater than six but if i

624.24

go back and do may 1st it's going to

625.76

give me you know because it's just the

627.279

month it really doesn't matter what day

628.72

whether i do may 1st

630.32

it's going to give me all

631.839

junes if i do may

634.8

28th it doesn't matter it gives me the

637.279

same thing because all it cares about

639.839

is the month part and if i did

644.839

day on those two

647.76

i think it's day and maybe date

649.92

we'll find out in a second yep it's

652.32

so here like since this is 5 28

656.8

the date's only going to be if it's a 29

658.72

30 or i guess a 31 if you were to have

660.8

one

664.72

so we're able to do some monkeying

667.12

around with our dates to do that if we

669.839

go back and look here and notice that

671.519

we've got we've been using our ands so

674.48

now

676.48

let's go back and do uh select distinct

680.399

age from horses

684.16

and let's do

686.079

so now let's come back here

688.399

uh

690.72

and we're going to take this same thing

693.839

and now let me just do this

699.04

what we want to do so let's say just age

701.279

is 15 or let's start with this

704.079

or

705.44

h equals 16. or let's say

709.04

well let's do that first

713.36

so if we do this

716.399

um

719.92

oops gotta do that

721.519

then we're gonna see

723.2

there's an h15 and then there's h16s now

726.16

there was a zero and let's try that

727.6

negative one so let's go back and say we

729.279

want to hit age 16 or

732.16

age negative 1 because

734.639

those are the two high ends

737.04

and let's just go that way now we could

738.72

say

739.68

you know greater than 14 for the other

741.44

one and we get 15 and 16. so let's try

743.36

these two

744.56

so if we do these we see that oh we get

746.88

a bunch of those

748.639

there's a bunch of negative ones let's

750.8

do

752.16

zero i didn't even count these so let's

754.24

see what we got here

759.2

that's not too bad okay so there's only

760.48

a few zeros so

763.36

i'll clean this up a little bit just

765.2

because

766.16

so we can do these two and we can get

767.68

our

768.72

zero

769.6

uh we can get our ages 16 and zero now

773.279

let's put this here

775.2

let's say we also want to do

778

and date created as greater than

782.72

pardon my throat there my clearing is

784.72

greater than 61 so that's going to be so

786.72

let's say that's 9 so let's do

790.8

greater than

794.079

4 18.

800

and what we're going to see

802.079

so let's look at this one

805.6

let's see what it gives us

808.399

let's do that

810.079

so now if we do it so now we say where

811.68

the age equals 0 or h equals 16 and date

814.24

created equals

816.079

is greater than 418

820.56

then we ended up getting

823.199

all of them

824.959

because

826.959

we have this or and we don't really have

828.88

it

829.839

logically set up because it can be

831.519

either the age of 0 or

833.92

the age of 16

836.16

and the and basically just gets sort of

838.72

lost because we've already hit one of

840.56

the ores

841.76

so instead what we need to do

844.48

is take that same one

848

and if we wrap it in parentheses

853.6

then we're gonna get this so now it's

855.04

going to be either the age is equal to 0

857.12

or 16 and the date's created equal to

860

that

863.04

did i do that right yeah i did that

864.48

right okay

867.839

is that date created yeah they created

869.92

so now

872.56

i only get those two

874.24

because it's saying it's either one of

876.399

these two ages

878.88

and

880.079

because it combines says with those two

881.76

then it also has to be tt created

884.88

so you need to be aware of parentheses

887.68

when you're dealing with complex where

890.079

clauses now sometimes people will

892.079

actually write these

894.32

in a way to make it a little easier to

895.76

read is they'll do some formatting be

897.36

something like you select the columns

899.04

you do from

900.32

and then what you're going to do is

902.24

you're going to have

905.04

each of the clause type pieces

908.56

and they may even do it like this so

909.92

it's a little easier for you to sort of

911.6

read that

912.72

now another thing that you could do is

915.04

which is not super uncommon i've seen

916.959

done is where you'll do like

918.959

a clause and another clause

921.839

so i could also say if i do and

926.639

let's say date updated

934.639

is greater than

937.04

518. so now if i take this

943.759

so now i'm going to say i want only

945.6

where the date updated is that so i'm

947.519

going to see that so now i've got that

948.88

those two ores gathered but then i've

950.56

got an and another and so i've got three

953.36

main things for the

955.279

query

956.399

and now i get that

959.04

but if it's an or

962

then i'm going to get something really

963.6

funky

969.36

and i would have to actually i'm not

971.199

sure exactly how to

973.68

plot that one out but because it's an or

976

there you go it's because

978.56

um

980

we have this or updated or date updated

982.32

that thing just like so these two get

984.72

basically combined and this one says

987.12

oh or if dt update is grown that which

989.519

means everything over here that i'm

991.36

getting is going to have a date uh a dt

993.92

updated greater than 518 or if it

997.44

doesn't

999.199

then

1000.48

it's got to be one of these other things

1002.399

then it's you know then it this is why

1004.48

it's hitting it because the age 0 or the

1006.24

age of 16 and the date created is that

1009.12

so again

1011.279

if i wanted to combine these two

1014.32

then i would have to do parentheses

1016.32

around it and now i'll get

1018.399

maybe nothing

1019.92

oh no i do because both have a

1025.039

oh where the date created is greater

1026.799

than that or

1028.079

the date updated is greater than that

1029.839

but so in either case i'm in pretty good

1032.799

shape

1034.24

um

1035.439

i can probably get a date updated late

1037.6

greater than 14. i don't know if i'll

1038.88

get another one let's try that real

1040

quick see if a different record shows up

1042.48

i haven't looked close enough quietly at

1044.559

my

1045.36

data nope get the same thing

1047.76

i think that's a good point to stop for

1049.28

this one

1050.32

we're going to get into a little bit

1051.2

more complicated stuff but really this

1053.28

this episode this lesson i want to just

1056.32

focus on

1057.6

just wanted to focus on ands and ors and

1059.76

making sure that you understand the

1061.039

parentheses matter

1062.799

so it's much less like logic you would

1065.2

see in any other programming language

1067.6

you know where there is some sort of

1068.72

boolean logic of ands and ors and

1071.52

equals and not equals and greater than

1073.6

less than and those sorts of things

1075.6

we've seen them a little bit

1077.12

and now i just want to get a little

1078.64

deeper next episode we will continue

1081.44

digging into where clauses

1084.24

but as always go out there and have

1085.84

yourself a great day a great week and we

1088.48

will talk to you

1090.24

next time

1091.89

[Music]

1107.44

you