Skip to playerSkip to main content
  • 1 day ago
Create the Database with this script: https://github.com/AlexTheAnalyst/PostgresqlYouTubeSeries

Practice Postgresql here: https://www.analystbuilder.com/questions

In this series I want to teach you Postgresql! It's one of the most popular types of SQL right now and is becoming even more popular with the rise of AI. I'll teach you all the syntax and how to use it as well as go beyond just querying!
____________________________________________

RESOURCES:

💻Analyst Builder - https://www.analystbuilder.com/

📖Take my Full MySQL Course Here: https://bit.ly/3tqOipr
📖Take my Full Python Course Here: https://bit.ly/48O581R
📖Practice Technical Interview Questions: https://bit.ly/46pDqqL

Coursera Courses:
Google Data Analyst Certification: https://coursera.pxf.io/5bBd62
Data Analysis with Python - https://coursera.pxf.io/BXY3Wy
IBM Data Analysis Specialization - https://coursera.pxf.io/AoYOdR
Tableau Data Visualization - https://coursera.pxf.io/MXYqaN

*Please note I may earn a small commission for any purchase through these links - Thanks for supporting the channel!*
____________________________________________

BECOME A MEMBER -

Want to support the channel? Consider becoming a

Category

📚
Learning
Transcript
00:00What's going on, everybody? Welcome back to another video. Today, we are continuing our
00:04PostgreSQL series. This is the very first lesson where we actually start querying data,
00:08and we're starting with the select and the from statement. Now, these are some of the
00:12most important ones because they select the data and they choose where that data is coming from.
00:17These are very important concepts. They're going to be in every single SQL query you write,
00:22basically ever, unless you're doing something like create table. But if you're querying data,
00:26you're going to have a select and from. That's just a fact. So understanding these concepts will
00:30last you a lifetime. So let's go ahead and take a look at how we can query this data.
00:35Now, we already created our tables in the last lesson. If you haven't done that already,
00:40I will leave a link to this right here. This is just our script for creating our tables
00:44and inserting our data. That's it. I will leave a link to this down below where you can download it
00:49from GitHub. Super easy. You just run it and then you're good to go. Now, let's actually get into
00:54the lesson. So what we need to do first is we need to write out select. And if you read
01:00that
01:01and you said something is wrong, I'm really proud of you. You're probably already ahead of the game
01:05and you know what's wrong here. Typically, when we have these commands in SQL, we write them in
01:10all caps. Now, it doesn't have to be. The query will technically run perfectly fine if it's all in
01:17lowercase. But it's kind of an unwritten rule of formatting your queries to have the commands in
01:22all caps. Now, I'm going to be doing that for 99% of the queries in this lesson. You don't
01:28have to.
01:28You can be a renegade and you can just do what you want. But that's what we're going to do.
01:33Now, let's go ahead and we're going to do select star. Now, what star means is it means everything.
01:39This means that if we select our character info or our planets or our ships, it's going to give us
01:44all the columns and all the rows of data. Now, this can be a little bit trickier as you start
01:49working with huge amounts of data because just selecting everything from a table could take
01:53minutes or, you know, an hour. If you have a ginormous data set, that'll just take forever to
01:59return. But we're going to select everything. Then we're going to go to the next row. And this is
02:03typically how you format a SQL query. Then we're going to write our next one, which is from. So we're
02:08selecting everything. But where are we actually selecting it from? We have to specify.
02:13So let's start with our character info table. So we're going to say character underscore info.
02:20And that's it. Select everything from character info. Let's go ahead and execute this script.
02:26And just like that, it says it ran successfully and it's returned all of our columns and all of
02:32our rows of data. Really briefly, let's just look at these columns. We have character ID, character name,
02:37species, planet ID, ship ID, estimated net worth, if they have both arms and their birth
02:44date. So this is our data, right? We have these columns. And in each column, we have different
02:50rows. That's each of these one, two, three, four, all the way down to 12. Now, when we're selecting
02:54from something, we can specify what we want to select. Now, there are a lot of different things
03:00included in the series. But even just in the select statement, there's a lot of things that
03:04we can do. For example, if we don't want to select everything, we don't want all of our columns,
03:09we can get rid of that star and we can specify the columns that we do want. For example, let's
03:15pull
03:16up the character underscore name and we can actually include multiple column names. All we have to do
03:21is separate it by a comma. So we're going to do character name and then we'll do, it's going to
03:26take
03:26a while. Estimated net worth. Let's go ahead and run this. And this looks perfect. So now we have
03:36specified different columns that we want. And in our output, that's all we have. Now let's come
03:42right down here. We're going to go below it. Because when I'm writing SQL queries, I typically
03:46don't just have one in here. I have multiple. I have three, four, five, six queries. And as it gets
03:52more advanced, they're kind of building on each other with temporary tables and CTEs and all these
03:57different things. It gets very complex. And so I typically don't just have one query. I have
04:02multiple. So let's come down here. Let's select everything from a different table. So we're going
04:06to do our ships table. So I'm going to do select everything from ships. Now this looks great, right?
04:15Let's go ahead and run this. Uh-oh, there is an error. And the error is not obvious at first.
04:20In fact,
04:21when I was first learning SQL many years ago, I was really confused about this. In a SQL editor,
04:27you can't have two queries run at the same time unless you have them separated. And you do that
04:32using a semicolon. So now that it has a semicolon between this code and this code, I'm calling it
04:38code, but it's, you know, just a query. Let's go ahead and execute this. And now we get our output.
04:44So it ran successfully. But you'll notice we only have one output down here. And that's fine.
04:50If we want to specify just this one, we can highlight over it. And then we can run it.
04:57And it's going to run this query instead. If we run the entire thing, it's just going to run
05:03the last query. So right here. Now typically at the end of all my queries, I put a semicolon just
05:09by default, just in case I do want to write something beneath it. But that's just extra information.
05:15I'm throwing stuff at you. But just extra information in case you want it. Let's bring
05:20this down a little bit. Something I do in the select statement a lot is I create new columns.
05:27And this is very, very common. Let's copy this. And I'm going to bring this right down here. And I'm
05:33just going to say select everything. And I only want to run this one. So let's go ahead and do
05:38that.
05:39But let's say I want to take their estimated net worth. And I just want to multiply it by
05:44something. That would be something that I can do in the select statement. So let's take
05:48estimated. And actually, let me copy this because I don't trust my spelling skills here. But I'm
05:55going to do a comma. And now we're going to do something. I'm going to take the estimated
05:59net worth. I'm going to do a star two. That's going to take the estimated net worth. It's going
06:04to multiply it times two. Now let's go ahead and run this. And right here, you'll see there's
06:11150,000. Now it's 300,000. You're also going to notice that there is no column name. It's
06:18question mark, column, question mark. And that is not helpful. So what we can actually do in the
06:23select column is use an alias. Now an alias just renames a column to something else. So what we're
06:31going to do is we're going to say as, and we can say double the worth. And this is going
06:39to rename
06:39this column as double the worth. Let's go ahead and run this. And there we go. So we rename that
06:46column to double worth. And now we have estimated net worth and double their worth. So we're able to
06:51perform calculations in the select statement as well. Now we don't have to do that with just this
06:57column because it didn't have a column name. We could do that with anything. So if we
07:01ran this query up here, we could rename this one and we could say as name. And if we run
07:08this one
07:08again, you'll see right down here. Now it says name. Now this gets really helpful the more advanced
07:14we get. So when we get to things like subqueries, CTE, window function, these are things where aliasing,
07:20not just in the select statement, but also in the from statement, aliasing becomes a lot more
07:25important. So we'll get to that in a future lesson to dive more into the advanced kind of pieces of
07:30aliasing, but we won't in this lesson. By now, you should feel really comfortable using select and
07:36from. Go ahead and try it out. Mess around with it. Select different tables, select different columns.
07:41And in the next lesson, we're going to start looking at the where statement where we can start
07:45filtering our data. If you learned anything in this lesson, be sure to like and subscribe,
07:49and I will see you in the next video.
Comments

Recommended