All right.
So we have this data in our Instagram clone database.
Now we're going to ask some questions.
And the reason I'm phrasing it that way, asking some questions is because that's usually what you're
actually doing.
If you work with SQL or SQL, some SQL database or any database, really, even if it's like a MongoDB
or coach DB post grass, my SQL, any database out there and you have a bunch of data in there?
Yeah.
Sometimes a lot of the time you're just sticking stuff in there.
But when you're actually working with the data in writing queries, a lot of what you're doing is asking
questions where you have a motivation, you're trying to figure something out, whether it's for an
ad campaign or a post mortem after a campaign.
I know I'm coming back to ads a lot.
It might just be sales like you could be a woodworker.
You make furniture and you want to figure out how much you sold last year because you're compiling your
tax returns or something and you need to figure out the total volume of sold.
Those are the questions you're asking.
So you think in terms of English or whatever language you speak, you think in terms of questions in
a non SQL language, like How many X did I sell last year?
And then you have to translate that to a some or a select something from something else and you have
to use some most likely anyways.
So that's how we're going to work here and we'll start out nice and simple in this first video.
Suppose that we want to reward the users who have been around the longest.
So you may have seen something like this on not an Instagram but other applications.
You'll sometimes get a coupon or just a thank you email.
Thanks for being loyal over our five years or however long we've been around.
So all we want to do is reward our users who've been around the longest.
And to do that we need to find the five oldest users, the users who have been around the longest.
So this one is should be simple, not crazy.
We don't need to work with many tables.
Short video.
Try this on your own.
Pause the video, of course.
And in 3 seconds, I'll be right back.
And I'm back.
So to do this, it's just a matter of basically selecting staff from users and then we just want to
order them based off of the date that they were created, the date they joined, and then we'll limit
it to five.
So if we hop over, what I'll do is make a new file.
New file?
Where are you?
And I'll just call it Solutions SQL.
And in here the first one is just finding we put the numbers here so it's easier for you when you're
looking at this, finding five oldest users and we know it's going to be a select something from users.
And if we start there, you can see we've got 100 users, which is one thing I wanted to point out.
100 users is not very much, but I capped it at 100 because basically the formula that I imprecisely
came up with for how often users are commenting and posting and liking and tagging, it grows exponentially
depending on how many users you have.
So having 100 users, we still have almost 20,000 records for everything else.
And if I upped it to 200 or I tried 1000 users at one point, it really started going crazy and our
queries take a lot longer.
So I decided not to do that.
But if you want to try, it could be fun to.
If you feel comfortable writing code to insert a bunch of users, do that in JavaScript or Python or
whatever language you like.
But for now, 100 users, it's just interesting to note how much data we get.
Even with just 100 users, if the average person is posting X number of times and then we get certain
celebrities who post all the time, not to mention people who are liking all the time.
And we get bots who like every single photo things quickly grow.
So select start from users but we want to order by and our field is called created at.
Right there.
So just order by created that.
And if we just do it this way, you'll notice that at the bottom we have today's date, basically roughly
today's date.
And then if we scroll up.
We've got the oldest one, so we've only been around for a little bit about a year according to the
data that I created.
So what we'll do is just limit five.
And there we go.
That's all we need.
So nice and simple for this one.
I want to start off with some review, but don't worry if this felt way too easy, that's.
That's good.
We're going to move on and get into some more exciting slash, potentially nastier territory.
All right.