0% found this document useful (0 votes)
26 views5 pages

57.8 - SELECT - mp4

sql select

Uploaded by

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

57.8 - SELECT - mp4

sql select

Uploaded by

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

Okay. As we learn more and more about SQL, right, let us have a real world context.

Let's
take a real world problem. Let's take a real world problem so that we understand SQL
better in the context of some real world problems, right? We have taken a real world data
set, like the IMDb data set here. So imagine, imagine you are a software engineer. Imagine
you are a software engineer, right? And your goal is to build a website. Let's assume you're
building a website. You're building a website or a mobile app where you want to show all of
this information that is there in this data set that we have and help people search for movies
that they may like. Let's assume that's what you're trying to do. You're building a website or
an app where you want people to discover movies that they think are interesting or to
understand more about this movies data that they have, right? Let's assume you're doing
that, okay? You're a software engineer, or you could even be a data scientist, or you could
even be a data scientist who is trying to understand the data that you have in this IMDb data
set. So we'll put things in this context so that you better understand how some of the
concepts that we are learning in SQL are actually applied in the real world. To be honest
with you, this whole website on which you are watching this video content, right? Or if
you're watching it on a website or a mobile app or a mobile browser, all of them use SQL
internally, and many of them may be using MySQL itself, like on our own website. We use
MySQL extensively as a backend database for most of our data storage needs, right? Okay.
Having said that. So let's take a task. We'll always try to put things in context of some real
world tasks. Suppose my first task is I want to list all the movies. I want to list all the movies,
right? I want to list all the movies in my database. In my database. I'll write database in
short as DB, right? If I want to list all the movies that are there and all the data I have about
these movies. Okay, we know that there is a table called movies. We've already seen its
description, right? So let's go and see the describe, describe movies, right? Okay, so my
movies table has a movie ID. It has a movie name, the year in which the movie was released,
and the rank score, which is often referred to as the IMDb score. IMDb scores each movie
between a rate range of zero to 1010, being the best movies, zero being the worst movies.
This is called the IMDb score. And this score is provided by movie viewers like us. And all
these scores are aggregated and an overall score is given. The range of all these scores is
between zero to ten. That's the data we have about these movies, right? So now the first
task that we have is to list all movies, right? And towards this task, what I have done here is
I'll share a very simple file here. This is a file that I've created so that this is like your notes.
Think of this like a notes with all the commands. And the reason I've written these
commands is so that I don't have typos or errors. I've already pretested some of these
queries so that there are no errors in this, right? We have already seen these three
commands earlier. Use database, show tables and describe a table. We have already seen
this in the previous videos. Right? Now we learn about a command called select command.
Very interesting command. I'll explain you what it does, right? So let's understand what the
select command itself does, right? Okay, let's run it. I'll explain you the syntax, everything
slowly, right? So let's take this command. So when you're running some of this, you can just
take this, copy this whole thing, control C and I go here, I paste. What is this command
literally doing? Right? So it's printing some information for me as soon as I gave it. So let's
understand this command first. Okay, so the command that I used is select star from movies.
Let me break it down for you. Right? Let me break this down for you. What did I do? I said
select star from movies. Okay, so select basically says, right? Okay, select basically says that
I want to select these columns. These are all the columns that I want to select and I want to
display that from. Basically is a keyword. See, select is a keyword in SQL. So is from, this is
the table name. This is the table name. So what it is literally saying is I want to select Star in
many computer languages, basically means everything. Star means everything, right? So
when I say select star from movies, what it means is select all the columns. This star
basically means select all the columns from movies. And there is a semicolon at the end like
all SQL queries. So what are you saying? Literally here you are saying I want to select all the
columns from the table movies. The moment you do that, okay, the moment you do this, let
me show you here, right? I'm clearing the screen with control L. What was select star from
movies. As soon as you type enter here, it will start printing out all the movies. All the
information that is there in the movies table. It's going to print everything that's there in the
movies table. It's going to print all the columns here, right? Very, very simple. Now, there is
one problem with this. So if you look at our tables, column. Sorry. If you look at our table
called movies, right? If you describe movies, we have four columns here we have four
columns here we have ID, movie name, movie year and movie rank score. But in reality,
there will be tables. There will be tables in the real world, right? With hundreds of columns.
With hundreds of columns. And when I just simply say, select star from movies, it's going to
print all the hundreds of columns which I may not need. And because you're printing all the
hundreds of columns even here, it's going to print all the four columns, right? And you may
not need all of the columns. Imagine. Imagine if a task. Imagine if our task was that you want
to see the name and the year of all the movies. If you want to list. You want to list the name
and year of all the movies in your database. You don't care here about ID. You don't care
here about the rank score. You only want the name and the year. Suppose if this is your
requirement, then how do we change. How do we change this? It's very simple. So we say
select. We say select. What all columns do we want here? We want only two columns. Name
and ear. So you say select name, comma, year. From which table you want movies. Right? So
here, what am I doing here? Select, basically says which columns to select. Right. Name and
year. These are the two columns that I want. I've not put a star. If I put star, it means all the
columns here. I want only these two columns from which is also a keyword. Which table?
Movies table. That's all right. Okay. So let's go and see this. Okay. Here, let's say. Suppose.
Okay, here, let's say I want name. And let's change this. Name and year. Sorry? Name and
year from movies. Okay. If I now enter this. Okay. Suppose if I enter this. Now it's only
printing two columns. If you notice, it's printing the name of the movie and the year in
which it was released. That's it. Right. It has printed all the 388,269 rows. Look at this. It has
print all the rows here. So if this was your table with four columns, you had ID, name, year
and rank score, right? Now, here I'm saying I want only the name and year. So it only prints
this information across all the rows. That's what it's doing. It's a very simple command,
right? And one thing you have to notice here is, remember here, we are not saying how to
print it. We are not explaining, we are not explaining, we are not explaining how to generate
output. We are not saying how to generate the output, right? As we discussed, SQL is a
declarative programming, programming language. If you want to think about it, right? It's
not a procedural thing. Here you just say what you want. Here you're saying I want the
columns, name and year from this table. It is the SQL engine, the query optimizer,
everything that takes care of how to do it, right? The parser, the compiler, everything that's
there within the database. You only say what you want. So you're saying I want to select
these two columns from this table. How you get it is none of my business. That's why SQL is
so simple, because you just say what you want without explaining how to generate the
output. That's very, very important, right? Another thing is, whenever you run any SQL
command, let's assume I run this select star from movies. Okay? So this generates, see at the
end of it, when you run this, right, it generates some output. That output, that output, that
output it generates is called a result set. It's called a result set. And what is a result set? If
you think about it, result set is nothing but another table that it creates. When I say select
star from movies, or when I say select name comma, year from movies, what it is
generating, the terminology in databases is called a result set. A result set is basically a set
of rows. It's a set of rows with column names, with column names. Or in other words, it's
actually a table that, it's generating a new table using this table. So when I said select name
comma, year from table, it is generating a result set for me, which consists of all the rows,
but only these two columns, right? So whenever I use the word result set, it basically means
it's a set of rows with column names. Or this is the output generated from a select query.
This is important to understand, right? There is one more small interesting detail here that
I'll go through. When I say select star from movies, I'm going to obtain all the columns while,
so this is, let's say case one. This is, let's say case two. So when I say select star from movies,
it's going to result all the columns. It's going to result all the columns because of the star
right now, which means the data that it will result, the amount of data that will result is
larger or the data is more than the data that this will result in. And hence this query two is
going to be faster. This query two is going to be faster than query one. So it's always advised
to only request for those columns that you need. Tables could have hundreds of columns.
I've seen tables with hundreds of columns in my own experience. There is no point in
running select star from movies because the amount of data that needs to be transferred.
Because every column data has to be transferred. Look at this. We had four columns here,
right? It has to return data from four columns, while here it has to return data only from
two columns. And this is far more optimal because the amount of data that you need to
transfer here is less in this query, the amount of data that you need to transfer is less than
query one. And hence query two is going to be faster. So it's always important to only query
for those columns that you need and not use star every time. Especially when you have
tables with lots of columns, right? That's a very, very important facet that you should keep
in mind when you're using the select SQL keyword or the select queries, right? Okay. Having
said that, there is one more important aspect that I wanted to go over again. I've provided
notes for all of this here. For example, if you see here, we say, okay, select star from movies
has more data transfer, right? And what is a result set? A result set is a set of rows that form
the result of a query, along with column names and some metadata about the table. Okay,
column names is also a metadata. Don't forget that column names is not the data in the
table, it's actually the metadata about the table. Metadata basically means data about the
data. Right. Similarly, now let's look at this, right? Okay. Again, the other interesting thing is
by doing this, I'm only obtaining two columns of data instead of all the columns. Now, again,
the order that you have here, the order of columns. Look at the order of columns that we
have here, right? Let's go to a terminal. Okay, so let me just clear the screen here. If I
describe movies, I have my columns. My first column is ID. My second column is name. My
third column is year. My fourth column is rank score. But I don't have to select columns in
the same order. For example, look at this. Okay, suppose if I take this query, let's say if I take
this query, select rank score, comma, name from movies, right? So let me type it here. I'll
copy pasting it here. It's running perfectly, right? So one interesting thing that you should
remember here is that my order of column names can be anything. My output. Like when I
run this, look at this. When I run this in my result set, my first column is going to be name.
My second column is going to be year. This is the table that I'll get with some data here. But
when I run this, my first column is going to be rank score. My first column is going to be
rank score. My second column is going to be the name of the movie and I'm going to get
some data. Look at what I have here. Look at what I have here. My first column, here are my
rank scores. My second column are the movie names, right? Again, it resulted in all the rows
that are there in the data set, right? So that's important. Very very important. So if you look
here, the order of the column names can be anything, okay? It need not be in the same order
as they are in the table. Because if you look here, if you look here, right? If you look here, if
you look here, the order that is there in the table is different. Here you have name before
rank score, right? But if you look here, the order here is different. The name score here is
before the name. The rank score here is before the name. So the output, the output result
set, the output result set, or the output table will have the order as mentioned here. And this
order can be anything. The only requirement here is this can either be star or it can be one
of the valid column names for the given table, right? That's one. The second thing here is
when I output all the rows. Look at this. Suppose my original table, my original table. Let's
assume this is my original table. Okay, let's assume this is my original table with ID, name,
let's say year and rank score. Okay, I'll just write rank score in short. Okay, let's assume I
have id one, id two, id three, so on and so forth. Okay, on top of this now let's assume I ran a
select query. I say select name, comma, year, name, comma, year from movies table. Let's
assume that's my select query. The output that is generated here, the output that is
generated here will contain what will contain only two columns, name and ear. The order in
which the rows appear here. The order in which these rows appear will be same as the
order in which the data is stored in the table. So first row will be output here, second row
will be output here. The first row, let's say name is n one, name is n two, name is n three. So
I'll have name one, name two, name three, right? And so on and so forth. So the order out
the of the output here, the order of the rows, the order of the rows is same as the order of
the rows that is there in the original table. For these simple select queries, we will look at
other select queries where the order will not be preserved. So these simple select queries
will preserve the row order. Okay, so this is called row order preservation because these are
very simple. What am I doing? I'm simply saying, get me these two columns from this table.
So of course, if ID one has name one here also you'll see name one, name two, name two
comes is a second. This row of belonging to name two comes after name one. Same here. So
the row order is preserved in simple SQL select queries. There will be other queries where
it will not be preserved. I'll explicitly mention that so that your understanding is deeper and
better. Okay, having understood this, now comes the next interesting question. Okay, so let's
go here. So we've understood the very, very basic, extremely simple select queries till now,
right? Very, very simple select queries. We have understood what star means or the fact that
we can use column names. We understood what is a result set here. We understood about
the row order preservation and we said it's always good to use column names instead of
using star because this will be faster because there is less amount of data that should be
transferred. Very, very simple stuff. Select is the most used and the simplest.

You might also like