📺 Develpreneur YouTube Episode

Video + transcript

SQL Tutorial - Day 7

2022-04-26 •Youtube

Detailed Notes

1. using aliases 2. select from select as source 3. create table from another table

select tutorial_parent.* from tutorial_parent

select tp.t_parent_id as pid,tp.t_one_id as t1id,tp.username as name,tp.email as mail,tp.start_yr as start from tutorial_parent as tp;

select tp.username,tp.email,tp.userLogin,t1.address_name,t1.city, t1.state,t1.zip, t1.type_id from tutorial_parent as tp,tutorial_one t1, lkp_type lt where tp.t_one_id=t1.t_one_id and lt.type_id = t1.type_id;

select * from stats s1 inner join (select * from stats where stats.value2 in (select max(value2) as value2 from stats group by value1) ) s2 on s1.stat_id = s2.stat_id;

select * from stats where stats.value2 in (select max(value2) as value2 from stats group by value1)

Transcript Text
[Music]
hello and welcome back we are continuing
our season where we're looking at our i
guess our series where we're looking at
sql particularly mysql and mariadb
and how we can improve our sql skills
this episode
we're going to continue digging into
some of the
insert type things creation and just
sort of managing our tables and such
the first thing we want to do
is
use like an alien the best way to do
this i'm going to have to show you is
we're going to use an alias and so if
you look at some of our prior
queries like select star from tutorial
parent
we get a record set but we can also do
let's give it an alias and we can either
do it with the space and then the name
and then we can do tp.star
whoops
and we're going to get the same set or
we can do as
to give ourselves an alias
we get the same thing now let's look at
if you look at the
selection here
you'll see
that we've got some default header
things and we can actually change those
around so instead of that we can use
select
let's do t parent
id comma t1
id comma
username comma email
comma
start year
from tutorial parent
and
whoops we're going to keep him as we did
use that alias now since we don't have
anything we don't have any conflicts
here we're okay we don't need to do a tp
dot in front of each of those
but for
clarity
that would be something we'd want to do
and i'm about to change this over
because this is a little slow on the
editing
so
let's play with this in our editor
whoops
come here
and do that
okay
so now you can see
that now
well our parent our names are the same
but now what i can do is i can say this
is gonna be uh
i'll do aspid
as t1 id
as name
as mail
as start
whoops
and now we will see
that here
we just reset our columns now this
becomes very important
when
ever we're doing uh whenever we're
dealing with a programming language and
pulling stuff out because inevitably
we're going to have this is our result
set
this thing right here
and when we pull values out it often is
going to be by name by column name
so we would want to pull you know mail
that column name we'd have to get that
value in order to bring it across if
it's something that's you know very
complicated for example
t parent id then there can be a problem
with that
and that becomes
a little bit of an issue
even with some complex queries so let's
take a look at one of those where we do
one of our prior
joins
and let's dig around a little bit and
take i think from let's see if we've got
one nope
and grab one just to save some time
he doesn't have one
here we go
oh here we go okay
and so we ran into this before
where we were pulling that we were
specifically stating values that we
wanted out of each of these
now if we have one that is unique let's
go back and pull this query real quick
so if we look at this query
then it comes up it gives us a username
email login address name c state zip but
now if we have
unfortunately
uh oh here we go
so for example we have
this type id exists twice
so we could do so if we said
i just want the type id
then we're going to get
whoops
oh i did we're going to get an error and
it's going to say that column's
ambiguous because it says hey that
exists in both
the
lt
and the t1 table but now if i come in
and i do t1
then i can say
that this
whoops this is the one i wanted so now
it sees it says that type id and it
doesn't tell me what the table is note
that even though i'm directing it from
various tables
it's just going to give me it the column
name is without the table prefix and
technically we could always do this
when we do this like select star
whoops if we do a select star from
tutorial parent
we could also do
select tutorial parent dot star
sometimes that's easier to read oops i
should use a semicolon sometimes it's
easier to read and sometimes not
particularly if you have a long
name now
i think i'll start the next one jump
into is creating a table from a table
and in so doing we're going to let's see
show
that shows show tables so i'm going to
go ahead because we've done this for a
while i'm going to change these names
i want to change the name of tutorial 1
which looks like this
and basically what i want to do is say
you know what this is going to be an
address
so i'm going to do create
table
address
as select star
from tutorial 1.
and when i do so
i can do describe address oh let me do
show tables real quick first
i should probably learn how to type
better
so i do show tables
and i don't have a typo we can see now i
have this address now if you look at it
let's look at
tutorial 1 where i created it
and then let's look at address
and you can see here
that the big difference is we don't have
an auto increment we don't have that
constraint those don't necessarily come
across and here we go
same thing with so we have that primary
key it's not set
and the
um the foreign key relationship that's
not set so we would have to actually go
in and do an alter table
address
alter column
t1
id and i want him to be let me just
steal some text from like probably here
uh nope
uh and let's just do him as
like this and i think i can ultra
channel one
let's try that
nope
oh
i wonder if it needs
may need this
oh it's not altar cow
is it
i'm gonna play for a second nope okay so
it is a calm
all right so let's not mess with that
right now simply because uh but it is
easier to do
uh alter column
actually uh oh
sorry it should be column
that's probably it
nope still not it
but
i do want to use
um
i'm going to go in and set as i did
probably somewhere
do i have an example of it
i do not
oh here we go
here we go so i can do
a constraint
and then he's gonna be
this will be uh
address pk
it's gonna be
i'm gonna call it
primary key
let me go steal that little thing from
probably day four
well day three
oh
i just have to do this
t1 id
see if that'll work yes it will so now
if i describe address
so now i can see where i have that
primary key i could probably go but i
think then i can go back and alternate
and make alter it make it that but i'm
not going to mess with that right now
because
what i can do whoops
what i could do then is i can do
tables and so if i do drop table
tutorial
one
oh it's because of the parent so i have
to deal with parent first now i'm going
to do create
a
table
[Music]
call it app user
as select
star from
tutorial parent
cronkite tutorial actually
oh
truncate tutorial one
up
helps to do that
now if i try to drop table tutorial one
up can't
let's go ahead and empty both of those
uh let's see tutorial one
it's tutorial parent
oh
now
there we go it takes a few minutes
to walk through all of those that's why
the constraints can be a bit of a pain
but they are also
useful to us stop saying shows i keep
using s's all over the
place and so now we have slightly
different looking
uh tables and we've sort of cleaned up
our names a little bit
and we can come back later and
add in uh adjust all of our constraints
that i will deal with at a later point
because
right now i want to jump into
so now we've got a couple tables we can
work from
and now i want to do a
a way to
get a little more complicated in our
queries so this one
let's start with
select star from stats
and so
whoops
so start with something like that but
what i can also do is i can interjoin
with other
tables or with other queries now i can
do it with itself
so i could do
stats i'm going to do s1
enter join
stats
s2 on
oh was that
s1 dot stat id
equals
s2 dot stat id
and
so if i do this i'm just going to see
the table twice
or the fields twice so i can see here
stat id so this is table one
this is table two
now this becomes a little more
interesting you know this is not
terribly
interesting but what we can do
is we can say instead of
stats
2 being that
now we can change it let's say we're
going to
select star from stats
and let's do uh
let's do this
actually we don't need to
yeah so let's do this we're going to do
select star from stats
can i do that
no i don't want to do that i want to do
this i want to do
let's do stat id
comma
max
uh let's pick max that we want to do
uh let's see max value
one
as
mx value
from stats
and then what i want to do is
group by
value
ah
let's see
oh wait i want to group by value one
and let's do max value 2.
so let's look at that first
so when we do this what we're doing is
we're going to say and we've seen this
before so
max value 495
here
for a value one
where it's value two it's gonna be 595
which we see
and here
it's 7985. now
uh for three but if you look over here
that stat id is just picking one it's
that idea of one it's not the max value
stat id 2 is not the max value
so what we would have to do
if we want to get the id
where the values the max we actually
have to come in here
and actually we can do it from here is
we can do select
star from
stats
where
uh was that value two
equals
or we're value two let's say n
we're gonna do this we're gonna do a
little intersect here
and we're just going to call them as
value 2.
uh let's see
and how is that going to look
uh see it's select stats start from
stats for value to and select max value
as value two from stats
group by value one
all right let's go look at that guy real
quick
oh
let's do this
okay so that's
that guy
so now what i want to do
let's do this
oh
where
stat step value 2
and that let's see if we can do that
this may not like it because i'm going a
little bit
stats where stats stop value too
oh
let's try this
my mistake
and there we go so now we're seeing that
we have the actual stat id so what we
can do here
is we'll take that and we'll place it up
here
well i guess we can and so we did it
this way instead of an inner join but we
can also take that same one
which is a complex query
as we can do here
and now we get this oh and now we're
getting both of them
we get our s1 and we get our s2 we get
our duplicate again
so we can do some pretty complicated
inner selects
and that's what we're going to continue
talking about next time i don't want to
run too late this time around
i got a little sidetracked on a couple
of things but we'll come back and we're
going to continue looking into these
but as always 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.16

hello and welcome back we are continuing

28.72

our season where we're looking at our i

30.4

guess our series where we're looking at

32.16

sql particularly mysql and mariadb

35.92

and how we can improve our sql skills

39.68

this episode

41.44

we're going to continue digging into

43.44

some of the

44.879

insert type things creation and just

47.2

sort of managing our tables and such

50.48

the first thing we want to do

54.239

is

56.48

use like an alien the best way to do

58.239

this i'm going to have to show you is

59.68

we're going to use an alias and so if

61.84

you look at some of our prior

65.84

queries like select star from tutorial

69.68

parent

74

we get a record set but we can also do

76.479

let's give it an alias and we can either

78.96

do it with the space and then the name

81.36

and then we can do tp.star

83.759

whoops

86.32

and we're going to get the same set or

88.479

we can do as

90.159

to give ourselves an alias

92.24

we get the same thing now let's look at

94.24

if you look at the

96.64

selection here

99.04

you'll see

100.72

that we've got some default header

102.399

things and we can actually change those

104

around so instead of that we can use

106.24

select

108.24

let's do t parent

110.24

id comma t1

113.2

id comma

115.52

username comma email

118.479

comma

119.759

start year

122.399

from tutorial parent

126

and

127.28

whoops we're going to keep him as we did

130.72

use that alias now since we don't have

133.44

anything we don't have any conflicts

135.12

here we're okay we don't need to do a tp

137.68

dot in front of each of those

140.16

but for

141.36

clarity

143.44

that would be something we'd want to do

147.2

and i'm about to change this over

148.879

because this is a little slow on the

150.959

editing

156.4

so

158.4

let's play with this in our editor

166.959

whoops

172.8

come here

175.12

and do that

177.92

okay

178.879

so now you can see

181.12

that now

182.4

well our parent our names are the same

184.8

but now what i can do is i can say this

187.2

is gonna be uh

189.519

i'll do aspid

193.76

as t1 id

199.44

as name

202.239

as mail

205.44

as start

207.28

whoops

211.2

and now we will see

213.68

that here

214.959

we just reset our columns now this

217.04

becomes very important

219.04

when

220.879

ever we're doing uh whenever we're

222.879

dealing with a programming language and

224.48

pulling stuff out because inevitably

226.159

we're going to have this is our result

227.519

set

228.4

this thing right here

230.48

and when we pull values out it often is

233.12

going to be by name by column name

236.56

so we would want to pull you know mail

240

that column name we'd have to get that

241.68

value in order to bring it across if

243.92

it's something that's you know very

245.439

complicated for example

247.439

t parent id then there can be a problem

249.68

with that

253.12

and that becomes

256.72

a little bit of an issue

258.479

even with some complex queries so let's

261.04

take a look at one of those where we do

262.88

one of our prior

264.56

joins

266.16

and let's dig around a little bit and

268.56

take i think from let's see if we've got

270.72

one nope

271.919

and grab one just to save some time

276.24

he doesn't have one

277.84

here we go

280.32

oh here we go okay

285.52

and so we ran into this before

291.44

where we were pulling that we were

294.24

specifically stating values that we

296.479

wanted out of each of these

299.199

now if we have one that is unique let's

301.44

go back and pull this query real quick

303.44

so if we look at this query

306.72

then it comes up it gives us a username

309.199

email login address name c state zip but

311.919

now if we have

313.84

unfortunately

317.68

uh oh here we go

319.759

so for example we have

322.08

this type id exists twice

325.44

so we could do so if we said

327.68

i just want the type id

331.44

then we're going to get

333.12

whoops

334.24

oh i did we're going to get an error and

336

it's going to say that column's

337.84

ambiguous because it says hey that

339.36

exists in both

341.12

the

342.08

lt

343.039

and the t1 table but now if i come in

345.6

and i do t1

347.759

then i can say

349.28

that this

351.28

whoops this is the one i wanted so now

353.6

it sees it says that type id and it

355.919

doesn't tell me what the table is note

358.4

that even though i'm directing it from

360.319

various tables

362

it's just going to give me it the column

363.84

name is without the table prefix and

366.88

technically we could always do this

369.36

when we do this like select star

375.12

whoops if we do a select star from

377.919

tutorial parent

381.68

we could also do

385.919

select tutorial parent dot star

390.72

sometimes that's easier to read oops i

392.24

should use a semicolon sometimes it's

394.08

easier to read and sometimes not

396.319

particularly if you have a long

398.24

name now

401.919

i think i'll start the next one jump

403.44

into is creating a table from a table

407.6

and in so doing we're going to let's see

409.599

show

410.639

that shows show tables so i'm going to

413.199

go ahead because we've done this for a

414.639

while i'm going to change these names

417.84

i want to change the name of tutorial 1

421.12

which looks like this

424.56

and basically what i want to do is say

425.68

you know what this is going to be an

426.72

address

428.639

so i'm going to do create

430.88

table

433.599

address

434.88

as select star

437.44

from tutorial 1.

441.599

and when i do so

444

i can do describe address oh let me do

446.56

show tables real quick first

448.88

i should probably learn how to type

450.96

better

452.4

so i do show tables

455.759

and i don't have a typo we can see now i

457.919

have this address now if you look at it

460.24

let's look at

462.319

tutorial 1 where i created it

465.919

and then let's look at address

470.24

and you can see here

472.16

that the big difference is we don't have

474.56

an auto increment we don't have that

476.96

constraint those don't necessarily come

479.039

across and here we go

480.879

same thing with so we have that primary

482.639

key it's not set

484.72

and the

486.639

um the foreign key relationship that's

488.879

not set so we would have to actually go

491.28

in and do an alter table

495.44

address

498.16

alter column

501.12

t1

503.68

id and i want him to be let me just

506.4

steal some text from like probably here

509.759

uh nope

518.24

uh and let's just do him as

522.24

like this and i think i can ultra

524.64

channel one

537.2

let's try that

540.08

nope

541.279

oh

542.399

i wonder if it needs

544.88

may need this

551.36

oh it's not altar cow

554

is it

556.72

i'm gonna play for a second nope okay so

560.08

it is a calm

562.56

all right so let's not mess with that

565.44

right now simply because uh but it is

568.08

easier to do

569.44

uh alter column

572.24

actually uh oh

578.24

sorry it should be column

583.279

that's probably it

584.72

nope still not it

587.44

but

588.32

i do want to use

590.88

um

594.399

i'm going to go in and set as i did

596.959

probably somewhere

600.48

do i have an example of it

604.399

i do not

606.24

oh here we go

609.76

here we go so i can do

612.88

a constraint

618.16

and then he's gonna be

621.92

this will be uh

624.32

address pk

627.44

it's gonna be

630.88

i'm gonna call it

636.24

primary key

638.32

let me go steal that little thing from

640.56

probably day four

642.64

well day three

646.72

oh

648.8

i just have to do this

650.6

t1 id

653.519

see if that'll work yes it will so now

655.279

if i describe address

659.44

so now i can see where i have that

661.36

primary key i could probably go but i

662.8

think then i can go back and alternate

664.48

and make alter it make it that but i'm

666.64

not going to mess with that right now

668.88

because

670.48

what i can do whoops

672.56

what i could do then is i can do

678.16

tables and so if i do drop table

681.6

tutorial

683.279

one

687.44

oh it's because of the parent so i have

689.2

to deal with parent first now i'm going

691.04

to do create

693.68

a

694.72

table

695.4

[Music]

697.04

call it app user

699.2

as select

700.839

star from

703.839

tutorial parent

709.44

cronkite tutorial actually

717.36

oh

718.56

truncate tutorial one

723.12

up

727.2

helps to do that

729.2

now if i try to drop table tutorial one

731.44

up can't

735.6

let's go ahead and empty both of those

738.56

uh let's see tutorial one

742.16

it's tutorial parent

746

oh

749.839

now

757.279

there we go it takes a few minutes

760.079

to walk through all of those that's why

761.6

the constraints can be a bit of a pain

763.519

but they are also

765.04

useful to us stop saying shows i keep

768.48

using s's all over the

770.839

place and so now we have slightly

773.44

different looking

774.72

uh tables and we've sort of cleaned up

776.32

our names a little bit

778.32

and we can come back later and

781.04

add in uh adjust all of our constraints

784.32

that i will deal with at a later point

786.959

because

788.32

right now i want to jump into

792.399

so now we've got a couple tables we can

793.839

work from

795.36

and now i want to do a

799.68

a way to

801.279

get a little more complicated in our

802.8

queries so this one

804.8

let's start with

807.12

select star from stats

810.8

and so

813.12

whoops

819.68

so start with something like that but

821.36

what i can also do is i can interjoin

824.48

with other

826.16

tables or with other queries now i can

828.399

do it with itself

830.32

so i could do

831.6

stats i'm going to do s1

834.48

enter join

838.16

stats

840.32

s2 on

842.639

oh was that

846.079

s1 dot stat id

848.959

equals

850.16

s2 dot stat id

854.839

and

856.56

so if i do this i'm just going to see

857.839

the table twice

859.68

or the fields twice so i can see here

861.519

stat id so this is table one

863.839

this is table two

866.8

now this becomes a little more

867.68

interesting you know this is not

869.6

terribly

870.88

interesting but what we can do

874.639

is we can say instead of

878.079

stats

879.12

2 being that

880.72

now we can change it let's say we're

882

going to

882.839

select star from stats

888.399

and let's do uh

892

let's do this

893.6

actually we don't need to

895.12

yeah so let's do this we're going to do

899.92

select star from stats

902.32

can i do that

904.32

no i don't want to do that i want to do

905.92

this i want to do

907.839

let's do stat id

911.12

comma

912.399

max

914.639

uh let's pick max that we want to do

917.76

uh let's see max value

920.24

one

926.56

as

928.56

mx value

931.12

from stats

933.12

and then what i want to do is

935.68

group by

939.04

value

940.48

ah

945.36

let's see

949.12

oh wait i want to group by value one

953.6

and let's do max value 2.

958.32

so let's look at that first

962.959

so when we do this what we're doing is

965.12

we're going to say and we've seen this

966.32

before so

968.8

max value 495

972

here

973.44

for a value one

976

where it's value two it's gonna be 595

978.88

which we see

981.04

and here

983.36

it's 7985. now

986.56

uh for three but if you look over here

989.68

that stat id is just picking one it's

992.24

that idea of one it's not the max value

995.759

stat id 2 is not the max value

999.44

so what we would have to do

1001.36

if we want to get the id

1003.04

where the values the max we actually

1006.88

have to come in here

1009.12

and actually we can do it from here is

1010.48

we can do select

1011.92

star from

1014.639

stats

1016.24

where

1018.24

uh was that value two

1024.319

equals

1026.959

or we're value two let's say n

1030.319

we're gonna do this we're gonna do a

1031.439

little intersect here

1038.24

and we're just going to call them as

1039.6

value 2.

1044

uh let's see

1045.6

and how is that going to look

1050

uh see it's select stats start from

1052.72

stats for value to and select max value

1055.039

as value two from stats

1057.919

group by value one

1061.039

all right let's go look at that guy real

1062.64

quick

1067.36

oh

1068.799

let's do this

1071.44

okay so that's

1073.36

that guy

1075.679

so now what i want to do

1078.08

let's do this

1082.96

oh

1084.64

where

1087.28

stat step value 2

1090.88

and that let's see if we can do that

1096.88

this may not like it because i'm going a

1098.559

little bit

1100.08

stats where stats stop value too

1112.16

oh

1113.76

let's try this

1119.2

my mistake

1121.76

and there we go so now we're seeing that

1123.6

we have the actual stat id so what we

1125.919

can do here

1127.6

is we'll take that and we'll place it up

1129.28

here

1133.76

well i guess we can and so we did it

1136.24

this way instead of an inner join but we

1137.679

can also take that same one

1139.6

which is a complex query

1143.039

as we can do here

1150

and now we get this oh and now we're

1151.679

getting both of them

1153.6

we get our s1 and we get our s2 we get

1157.039

our duplicate again

1159.84

so we can do some pretty complicated

1161.919

inner selects

1164

and that's what we're going to continue

1165.52

talking about next time i don't want to

1167.039

run too late this time around

1169.12

i got a little sidetracked on a couple

1170.88

of things but we'll come back and we're

1172.16

going to continue looking into these

1174.16

but as always go out there and have

1175.679

yourself a great day a great week and we

1178.16

will talk to you

1179.76

next time

1196.16

you