📺 Develpreneur YouTube Episode

Video + transcript

SQL Tutorial - Day 6

2022-04-21 •Youtube

Detailed Notes

Tutorial notes:

1. Where options = null not null and greater or less than 2. order by 3. group by 4. stat table

create table stats( stat_id INT NOT NULL AUTO_INCREMENT, code varchar(4), value1 int, value2 int, PRIMARY KEY ( stat_id ) ); insert into stats(code,value1,value2) values ('A',1,95); insert into stats(code,value1,value2) values ('B',2,195); insert into stats(code,value1,value2) values ('C',1,295); insert into stats(code,value1,value2) values ('ABC',2,395); insert into stats(code,value1,value2) values ('ABCD',1,495); insert into stats(code,value1,value2) values ('ABCE',2,595); insert into stats(code,value1,value2) values ('ABEC',3,695); insert into stats(code,value1,value2) values ('AEBC',3,795);

select code,value1,max(value2) from stats group by value1 order by value1; select * from stats where code in ('A','ABC');

Transcript Text
[Music]
well hello and welcome back we are
continuing our series of uh tutorials
about
my sequel maria db really trying to keep
it general
sequel but you know sometimes we do
manage to sort of split into a couple of
things that are more or less specific to
my sequel mariadb particularly if you're
using a higher end
more like you know super full-featured
situation like a
quicken or not quick oracle or
sql server where they do have their own
extensions but we're going to try to
keep it as useful as possible and
continuing this episode where we're
going to look into
we're going to like dig into some query
type stuff this time
so we're going to talk about where
clauses
and i think we're going to start with
let's start with
our basic stuff so when we had if you go
back to our tutorial parent if we select
everything from there
we have four rows
and
what we've seen before for the most part
is a select although we've used inner
joins and outer joins but we're now
going to talk about
within
our query and we'll start with something
simple so we're going to start about a
single table
now in this one
let's say that we only want
everybody that has a creation uh let's
start with this uh only one has a t1 id
of three
now we could there's a couple ways we go
about it but with the where clause the
easiest way is we're gonna say select
star from
tutorial parent
where
and then we just give it the column name
so we say t
one id
and then we say equals three so
everywhere it equals three give me the
records bam we have our two records
now we can also do greater than so i
could say uh where it is
let's say less than four
now notice we get three records back
because null
is not less than four
is it greater than uh is it greater than
zero let's find out
nope it is also not greater than zero
so it's important to note here that
because we're using greater than or less
than on the
uh this energy value it doesn't
it doesn't know where to sort or where
to put
as far as greater than or less than a
null so it just ignores it it's like
nope i'm not even going to think about
that row
but note here that if i do where t1 id
is less than 4 it's going to give me all
of these if i just do
if it's less than 3
then it's just going to give me the only
one
where the id is less
than now i can also do if i go back to
my equals i can do a not equals
and this is again note that even though
i do not equals null
is not
which makes it double negative is not
not equal to 3.
now i could say
what if i say if it's not equal to null
then i get nothing because whoops
i get nothing
because this is a numeric representation
this is a numeric not equal when i want
to deal with null i would have to say it
either is or it is not so if i say where
everywhere a word is null
then i get my record
if i say everywhere where it is not null
then i'm going to get all of these other
records
so i'm able to
do a lot of your basic stuff that you
would think of you know if you do
greater than less than nulls are either
going to be they either it either they
they are it's either they is or they is
not so it either they are or they are
not
and you can also do
uh let's see what else should we want to
make sure i get what we want to
uh no not no equal
greater than less than not equal
let's now what we can also do with our
query let's go back to uh we'll keep it
simple so if we go back to select star
one of things we can do is we can
actually order
the results
like here
um the paranoid it looks like it's sort
of in like a natural order of where the
record is but that's actually not
guaranteed what i could do is i could
say let me start with the lowest to the
highest of the ordered ids
and that is called order by so and
that's after the where clause if you
have one
so it's basically one of the last things
you do is you say order by and then i
give it the column name
so i'm going to order by t1 id
and when i do that
now here
null comes first
it takes the nulls first and then
it does them in order for t1 id
and that's going to be
and there's two different ways you can
do it you can do ascending
which is asc and that's what is by
default or
i can go in descending order
and now the threes are at the top the
one and then the nulls at the bottom so
note that when you're doing although
lesser than and greater than
does not
with a number take into account no
when you do an order by null is less
than any number
so if i had some i mean i could
i could play around with the numbers and
put like a negative in there or
something like that and null is still
going to be there now
i can also do um
[Music]
now some of these are going to be pretty
straightforward because if i do
if i do order by
user login
make a string
then you can see that it's ordered in
that fashion luckily or unfortunately
maybe in my case since i have these in
different ways
i have those number the names ordering
the same as ids that could be a problem
but let's look at tutorial one
whoops
and play around with that a little bit
because i think that one's different
yeah so here you can see by default it
goes by t1id
if i want to go by state
then i'm going to see s234
i can order by zip
that's going to be the same order shoot
i did the same thing in both of those
but
note that now
have completely nothing to do with the
t1 id column because that's in a
completely reverse order
so i can do an order by i can also do
a thing called a group by and so
when i go back to
parent
then
i can come in
and i can do group by
um start year
and now what it does
is these are sort of like roll-ups
it's just going to grab a record because
we only have two start years we have 20
22 and then we have dolls for these two
and so it's only showing me
groupings for these
and the rest of data sort of doesn't
seem to matter very much
but that's because i'm not doing
grouping functions
so in order to look at those let's play
around a little bit we're going to
create yet another table
and this one we're going to call stats
and all it's got is an id
and then there we go
uh an id a code and then two integer
values
and then what i want to do is i'm going
to take
i'm just going to go ahead and insert it
whoop what was my code
code was for oops
let me do this
i didn't do that right
so let me take these three
there we go okay so now if i do select
star from stats i'm going to see a lot
of stats
so i've got eight rows
now
if i want to do a
um
and of course i didn't uh i probably
didn't do this right for my total group
buys
i can
let's do this
that's
i'm going to change these real quick so
i'm going to delete from stat actually
it's truncate stats
so i'm going to empty the table
and instead i'm going to come in and i'm
going to change these first values so
let's get one
two
one two
let's do three and three okay
so now let's take that and insert those
because i needed some repeating values
which i do i have so now if i just like
start from stats
okay
so i can see and let's do order by value
one just so it's easy to see that or by
value one
so i can see i can have i've got three
that have the same value one two are
threes with a two and two with 3. now if
i do group by
and actually order by is the last thing
so i'm going to keep it that way but i'm
still going to i'm going to add my
group by
value 1.
and so i still see it in order but now
it's basically just taken the first
record it finds as you can see for each
of the values
now i can actually do something
interesting with that so instead of just
doing the normal star i'm going to do
i select code because remember we can
tell it which
columns we want so let's go ahead and do
these columns
so i've dropped my id out now what i
want to do
is instead of showing that value i'm
going to do a sum
of the values
of value 2.
and so here
i should see 95 plus 295 plus 6 4.95 or
for the twos i should see one three and
595 added and then here
i'll see six plus seven
if we do that now we can see that i've
got these
totals and so now i'm actually
generating in my query i'm generating
totals of like values
the same thing is i can instead of a sum
i can do a max
and now it shows me the maximum value
for each of these i can do a min
and show the minimum value
let me take this and copy it so just
make sure it's in the notes
and
yeah that'll work
uh and so men we can see that it's the
minimum the 95 where'd they go
95
195. 695
and now we can also do
let's go back to our where's
because that gives us some basics for
doing some group buys in order by now we
can group
across multiple fields
but we're not going to get into that yet
but i do want to do is i'm going to go
back to
i'm just going to select star from stats
and now i'm going to do where
because there's another thing that i
think is very useful for us to get i'm
going to wear code let's say where code
equals
a
so if we do that we get that one value
back
if we do abc
we get the abc record
now we can do
a list of values so we could say where
code in
and give it parentheses and give it the
list of values
so if i do n a and in abc
which is these two you know where it's
in a
and also abc
then we can see those two records
so let me make sure i capture that for
our notes
and then we can also do a like so now i
can say where code
like now if i want to do work code like
a
it's only going to give me an a because
it says
like is it there's not really any
ambiguity it's going to be the same as
an equal but let's say i put a percent
which is a wild card after the a
this says give me everything where it
starts with a
now
i get all these records
so let me go look at the e i don't think
we started with the d anywhere we did
not start with an anywhere but we do see
an e sort of floating around
so here
nothing starts with an e
but we do have we can put a percent
before that and say is there anything
that ends with an e yes as a matter of
fact there is
now the other thing is we can say give
me a record where e exists anywhere
in the code
and now i get it whether it's at the end
the middle or there
now i can do some other stuff because i
could say
let's see
i can do multiple strings here so i
could say let's say it has to
have b and e next to each other
b first followed by e
there is that case
um i can reverse it
and say eb
but now i can't put a percent between
those and get both
because it's still going to precede the
e
by from the b so we can place the that
wild card wherever within our
our string that we're looking for now
note that this is uh this is not a
number so if we wanted to do
uh value two
like let's say seven percent
say it starts with the seven
oh i lied it is gonna allow that
so i can do that within my numbers
so even though these are and those are
um
those are integers but we are okay with
that so we can find some things that are
so basically let's go back to look at
like our
uh let's go look at
our data here
and let's see if we can find something
so let's say we want to look for a zip
code
i granted that strings but still so you
could also look for
any zip code that
well we sort of played around with that
so i guess that's not
as good
let's see if we get something different
here
we may revisit this just to get some
funky data oh let's try this let's play
around with it with um it's like star
from
where
uh let's see are those yeah so update
date like let's take a look at this one
uh
percent oh nine
and we're going to see that we've got
that date so we could also do it like
for every date
0409
or if we wanted to start it then we
could go 2022
there we go and that's going to give us
all of those dates
so we can play around with this quite a
bit as far as i like it gives us just a
yet another
and also a very powerful way to do our
searches
uh or our where clauses
the warning i would say about a like
as opposed to
just about every other way to grab data
is that it's not as index friendly so if
you're building big complex queries
you're taking a lot of data back
then or such i guess it's more like if
you're searching through a table with a
lot of data
then it can take longer to do the like
quite a bit sometimes than to do a
a greater or less sam and we'll talk
more about indexes as we go further into
this
this discussion but i think that's
probably pretty good place for now for
us to wrap this one up
so we've
we've played around quite a bit now with
some very basic uh queries we've built
some tables we've got some relationships
and we'll just keep on uh keeping on in
the next episode around so hope you go
out there and have yourself a great day
a great week and we will talk to you
next time
you
Transcript Segments
0.48

[Music]

26.24

well hello and welcome back we are

28.84

continuing our series of uh tutorials

32.96

about

33.92

my sequel maria db really trying to keep

36.399

it general

37.84

sequel but you know sometimes we do

40.239

manage to sort of split into a couple of

42.96

things that are more or less specific to

46.079

my sequel mariadb particularly if you're

48.399

using a higher end

50.239

more like you know super full-featured

53.28

situation like a

55.12

quicken or not quick oracle or

58.719

sql server where they do have their own

61.6

extensions but we're going to try to

63.359

keep it as useful as possible and

64.799

continuing this episode where we're

66.799

going to look into

68.479

we're going to like dig into some query

70.24

type stuff this time

72.96

so we're going to talk about where

74.88

clauses

76.08

and i think we're going to start with

78.159

let's start with

80.24

our basic stuff so when we had if you go

83.2

back to our tutorial parent if we select

86.799

everything from there

88.56

we have four rows

90.88

and

92.079

what we've seen before for the most part

94.159

is a select although we've used inner

96.24

joins and outer joins but we're now

98.159

going to talk about

99.439

within

100.799

our query and we'll start with something

102.479

simple so we're going to start about a

103.759

single table

105.68

now in this one

107.68

let's say that we only want

110.24

everybody that has a creation uh let's

113.52

start with this uh only one has a t1 id

116.399

of three

117.68

now we could there's a couple ways we go

119.04

about it but with the where clause the

120.719

easiest way is we're gonna say select

122.799

star from

124.159

tutorial parent

127.439

where

128.879

and then we just give it the column name

130.16

so we say t

132.08

one id

133.84

and then we say equals three so

135.52

everywhere it equals three give me the

136.959

records bam we have our two records

140.239

now we can also do greater than so i

142.56

could say uh where it is

146.16

let's say less than four

149.76

now notice we get three records back

152.72

because null

154.319

is not less than four

156.64

is it greater than uh is it greater than

159.04

zero let's find out

161.28

nope it is also not greater than zero

163.92

so it's important to note here that

165.44

because we're using greater than or less

167.84

than on the

169.44

uh this energy value it doesn't

172.319

it doesn't know where to sort or where

174.239

to put

175.28

as far as greater than or less than a

176.72

null so it just ignores it it's like

179.04

nope i'm not even going to think about

180.159

that row

181.599

but note here that if i do where t1 id

184.8

is less than 4 it's going to give me all

186.72

of these if i just do

188.72

if it's less than 3

192.56

then it's just going to give me the only

194.08

one

195.12

where the id is less

196.84

than now i can also do if i go back to

199.36

my equals i can do a not equals

203.92

and this is again note that even though

205.519

i do not equals null

208

is not

209.36

which makes it double negative is not

211.92

not equal to 3.

214.72

now i could say

216.879

what if i say if it's not equal to null

220.239

then i get nothing because whoops

223.2

i get nothing

224.72

because this is a numeric representation

227.519

this is a numeric not equal when i want

229.599

to deal with null i would have to say it

232

either is or it is not so if i say where

234.4

everywhere a word is null

236.4

then i get my record

238.4

if i say everywhere where it is not null

242.879

then i'm going to get all of these other

244.159

records

246.799

so i'm able to

249.28

do a lot of your basic stuff that you

250.959

would think of you know if you do

251.92

greater than less than nulls are either

254.159

going to be they either it either they

256.72

they are it's either they is or they is

258.639

not so it either they are or they are

260.799

not

262.639

and you can also do

265.199

uh let's see what else should we want to

266.96

make sure i get what we want to

269.28

uh no not no equal

272.24

greater than less than not equal

275.36

let's now what we can also do with our

277.919

query let's go back to uh we'll keep it

280.56

simple so if we go back to select star

283.36

one of things we can do is we can

284.88

actually order

286.639

the results

288

like here

289.52

um the paranoid it looks like it's sort

291.44

of in like a natural order of where the

293.36

record is but that's actually not

294.72

guaranteed what i could do is i could

297.44

say let me start with the lowest to the

299.84

highest of the ordered ids

302.96

and that is called order by so and

305.52

that's after the where clause if you

307.28

have one

308.24

so it's basically one of the last things

309.68

you do is you say order by and then i

311.919

give it the column name

313.52

so i'm going to order by t1 id

318.8

and when i do that

321.12

now here

322.639

null comes first

324.639

it takes the nulls first and then

328.16

it does them in order for t1 id

330.88

and that's going to be

333.12

and there's two different ways you can

334.24

do it you can do ascending

337.039

which is asc and that's what is by

339.28

default or

341.44

i can go in descending order

346

and now the threes are at the top the

349.039

one and then the nulls at the bottom so

350.8

note that when you're doing although

353.28

lesser than and greater than

355.12

does not

356.4

with a number take into account no

360.08

when you do an order by null is less

362.72

than any number

364.72

so if i had some i mean i could

367.039

i could play around with the numbers and

368.08

put like a negative in there or

369.039

something like that and null is still

370.4

going to be there now

373.44

i can also do um

375.62

[Music]

378

now some of these are going to be pretty

379.199

straightforward because if i do

381.84

if i do order by

385.759

user login

389.919

make a string

391.759

then you can see that it's ordered in

394.24

that fashion luckily or unfortunately

396.88

maybe in my case since i have these in

398.72

different ways

400.479

i have those number the names ordering

402.8

the same as ids that could be a problem

404.96

but let's look at tutorial one

407.6

whoops

410.479

and play around with that a little bit

411.599

because i think that one's different

412.479

yeah so here you can see by default it

415.12

goes by t1id

416.72

if i want to go by state

424.4

then i'm going to see s234

427.36

i can order by zip

429.52

that's going to be the same order shoot

430.8

i did the same thing in both of those

432.479

but

433.52

note that now

435.44

have completely nothing to do with the

437.039

t1 id column because that's in a

438.8

completely reverse order

443.199

so i can do an order by i can also do

446.88

a thing called a group by and so

451.199

when i go back to

453.36

parent

459.52

then

460.56

i can come in

462.319

and i can do group by

465.68

um start year

471.44

and now what it does

473.919

is these are sort of like roll-ups

477.12

it's just going to grab a record because

479.599

we only have two start years we have 20

481.28

22 and then we have dolls for these two

484

and so it's only showing me

486.4

groupings for these

488.96

and the rest of data sort of doesn't

491.52

seem to matter very much

494.879

but that's because i'm not doing

497.599

grouping functions

499.199

so in order to look at those let's play

501.28

around a little bit we're going to

502.72

create yet another table

505.199

and this one we're going to call stats

507.759

and all it's got is an id

510.8

and then there we go

512.8

uh an id a code and then two integer

515.919

values

517.599

and then what i want to do is i'm going

519.2

to take

522.88

i'm just going to go ahead and insert it

525.68

whoop what was my code

528.88

code was for oops

534.48

let me do this

535.92

i didn't do that right

538.56

so let me take these three

541.36

there we go okay so now if i do select

543.92

star from stats i'm going to see a lot

545.519

of stats

546.959

so i've got eight rows

549.279

now

550.88

if i want to do a

553.44

um

557.04

and of course i didn't uh i probably

559.76

didn't do this right for my total group

561.68

buys

564.16

i can

565.68

let's do this

567.12

that's

568.48

i'm going to change these real quick so

569.839

i'm going to delete from stat actually

571.839

it's truncate stats

575.44

so i'm going to empty the table

578

and instead i'm going to come in and i'm

579.839

going to change these first values so

581.279

let's get one

583.04

two

584.8

one two

587.839

let's do three and three okay

590.48

so now let's take that and insert those

594.24

because i needed some repeating values

595.76

which i do i have so now if i just like

598.48

start from stats

601.36

okay

602.32

so i can see and let's do order by value

604.32

one just so it's easy to see that or by

607.6

value one

610.24

so i can see i can have i've got three

612.959

that have the same value one two are

615.44

threes with a two and two with 3. now if

618.24

i do group by

622.64

and actually order by is the last thing

624.48

so i'm going to keep it that way but i'm

626.079

still going to i'm going to add my

627.76

group by

630.16

value 1.

633.519

and so i still see it in order but now

635.519

it's basically just taken the first

636.8

record it finds as you can see for each

639.2

of the values

640.959

now i can actually do something

642.64

interesting with that so instead of just

644.32

doing the normal star i'm going to do

647.2

i select code because remember we can

649.12

tell it which

651.12

columns we want so let's go ahead and do

652.72

these columns

655.519

so i've dropped my id out now what i

657.519

want to do

658.959

is instead of showing that value i'm

662

going to do a sum

664.959

of the values

666.24

of value 2.

667.839

and so here

669.68

i should see 95 plus 295 plus 6 4.95 or

674.16

for the twos i should see one three and

676.24

595 added and then here

679.04

i'll see six plus seven

680.8

if we do that now we can see that i've

682.72

got these

684.56

totals and so now i'm actually

686.32

generating in my query i'm generating

688.399

totals of like values

691.12

the same thing is i can instead of a sum

695.92

i can do a max

698.399

and now it shows me the maximum value

700.48

for each of these i can do a min

704.959

and show the minimum value

709.44

let me take this and copy it so just

710.959

make sure it's in the notes

719.04

and

720.48

yeah that'll work

722.16

uh and so men we can see that it's the

724.24

minimum the 95 where'd they go

727.68

95

728.839

195. 695

732.399

and now we can also do

735.76

let's go back to our where's

737.92

because that gives us some basics for

739.36

doing some group buys in order by now we

741.2

can group

743.36

across multiple fields

745.839

but we're not going to get into that yet

747.6

but i do want to do is i'm going to go

749.2

back to

752.32

i'm just going to select star from stats

760.16

and now i'm going to do where

762.48

because there's another thing that i

763.519

think is very useful for us to get i'm

765.2

going to wear code let's say where code

767.2

equals

768.399

a

769.76

so if we do that we get that one value

772.56

back

774.079

if we do abc

778.399

we get the abc record

781.519

now we can do

784.24

a list of values so we could say where

786.56

code in

788.48

and give it parentheses and give it the

790.48

list of values

791.92

so if i do n a and in abc

796.639

which is these two you know where it's

798.56

in a

799.519

and also abc

801.279

then we can see those two records

804.399

so let me make sure i capture that for

806.399

our notes

809.68

and then we can also do a like so now i

812.079

can say where code

814.48

like now if i want to do work code like

816.88

a

818.56

it's only going to give me an a because

820.48

it says

822.72

like is it there's not really any

824.8

ambiguity it's going to be the same as

826.56

an equal but let's say i put a percent

830.16

which is a wild card after the a

832.88

this says give me everything where it

834.959

starts with a

836.399

now

837.68

i get all these records

840.079

so let me go look at the e i don't think

842.72

we started with the d anywhere we did

844.24

not start with an anywhere but we do see

846.56

an e sort of floating around

849.519

so here

852.16

nothing starts with an e

854.72

but we do have we can put a percent

856.959

before that and say is there anything

858.639

that ends with an e yes as a matter of

860.48

fact there is

861.839

now the other thing is we can say give

863.519

me a record where e exists anywhere

866.56

in the code

868.16

and now i get it whether it's at the end

869.92

the middle or there

872.24

now i can do some other stuff because i

873.839

could say

875.44

let's see

876.88

i can do multiple strings here so i

878.56

could say let's say it has to

882.24

have b and e next to each other

884.959

b first followed by e

886.959

there is that case

889.76

um i can reverse it

892.24

and say eb

895.44

but now i can't put a percent between

897.12

those and get both

899.199

because it's still going to precede the

900.959

e

902.399

by from the b so we can place the that

905.04

wild card wherever within our

907.839

our string that we're looking for now

910.72

note that this is uh this is not a

913.519

number so if we wanted to do

916

uh value two

921.6

like let's say seven percent

923.92

say it starts with the seven

926.639

oh i lied it is gonna allow that

929.6

so i can do that within my numbers

939.04

so even though these are and those are

941.839

um

945.68

those are integers but we are okay with

947.839

that so we can find some things that are

950.079

so basically let's go back to look at

951.68

like our

954.16

uh let's go look at

961.6

our data here

965.04

and let's see if we can find something

966.48

so let's say we want to look for a zip

968.56

code

970.48

i granted that strings but still so you

972.48

could also look for

974.32

any zip code that

976.32

well we sort of played around with that

977.6

so i guess that's not

979.279

as good

980.48

let's see if we get something different

981.759

here

984.16

we may revisit this just to get some

985.92

funky data oh let's try this let's play

988

around with it with um it's like star

991.279

from

992.88

where

994.8

uh let's see are those yeah so update

999.44

date like let's take a look at this one

1002.959

uh

1005.839

percent oh nine

1008.959

and we're going to see that we've got

1010.399

that date so we could also do it like

1011.92

for every date

1014.399

0409

1018.56

or if we wanted to start it then we

1020.399

could go 2022

1022.88

there we go and that's going to give us

1024.319

all of those dates

1026.48

so we can play around with this quite a

1028

bit as far as i like it gives us just a

1031.52

yet another

1032.799

and also a very powerful way to do our

1036.24

searches

1037.36

uh or our where clauses

1040.16

the warning i would say about a like

1043.199

as opposed to

1044.799

just about every other way to grab data

1048.079

is that it's not as index friendly so if

1051.76

you're building big complex queries

1054.16

you're taking a lot of data back

1056.08

then or such i guess it's more like if

1058.559

you're searching through a table with a

1060.24

lot of data

1061.76

then it can take longer to do the like

1064.64

quite a bit sometimes than to do a

1068.32

a greater or less sam and we'll talk

1070.24

more about indexes as we go further into

1072.72

this

1073.52

this discussion but i think that's

1075.44

probably pretty good place for now for

1076.96

us to wrap this one up

1080.16

so we've

1081.6

we've played around quite a bit now with

1083.039

some very basic uh queries we've built

1086.48

some tables we've got some relationships

1089.36

and we'll just keep on uh keeping on in

1091.919

the next episode around so hope you go

1094.08

out there and have yourself a great day

1095.76

a great week and we will talk to you

1098.559

next time

1114.799

you