SQL Tutorial for Beginners 4: SELECT - Retrieving Records from Your Database
Key Takeaways
This video tutorial covers the basics of SQL, specifically the SELECT statement, WHERE Clause, and ORDER BY statement, to retrieve and filter records from a database.
Full Transcript
hey everybody how's it going in this video we will be learning how to retrieve data from our database using the select statement now in order to have more data to work with than we had in our previous videos I created a table here called people that has 20 sample random records so this table has this ID column first name last name age and occupation so let's go ahead and get started and see how we can retrieve values from this table using the select statement so first let's just grab all of our records from the table now we can do this with the query select star from people now what's going on here is we're doing select and then we want to specify which columns that we want and if we put a star in then we're just saying that we want everything so we're saying select everything and then we need to tell it where we want everything from and we want it from the table people so if I run this query you can see that we get back all 20 records from our people table so what if instead of all the columns we just wanted the first name and the last name well instead of this star to get all of the columns we can just do first name and then comma last name and if we run this query you can see that now we get all 20 records but we only get the first name and the last name columns so this is great so far but how do we actually filter the results down to records that meet a certain criteria um so we can do this with the wear Clause so let's say I want to retrieve all the records from the people table where the last name is equal to do well to do that I can just add in this wear Clause here then I can say where last name is equal to and then within quotes here do so now if I go ahead and run this then you can see that we got all of the records back where the last name is equal to do now we do need these quotes around uh the last name here because the last name column contains string characters now what if we wanted to retrieve all the records from our database where the last name was either equal to doe or Smith well we can do this by adding an or to our wear Clause so here where we have last name equal to do and on the next line I can add in or last name equal to Smith so if I run this now you can see that we get all of the results back where the last name is equal to do or the last name is equal to Smith so now looking over here in the age column what if I wanted all the records in our table where the last name was equal to do and the age was less than 30 uh so we can do this by adding an and to our w Clause so instead of this or here I'm going to do an and age less than 30 so if we run this we can see that now we only get two results back instead of three and all of the ages are less than 30 so let's quickly look at the difference between the and statement and the or statement so the and requires that both conditions are met so in this example the last name and the age conditions are both met uh if I was to replace this here with an or and ran this query you can see that now it returns all of the records where either of these conditions are met so now instead of returning all of the records where the last name is equal to do and they're less than the age of 30 it's returning all of the records where the last name is equal to do or the age is less than 30 so you can see here that we have a last name with Smith where the age is less than 30 and it got included because we using the or statement so now let's take a look at how we can sort the results that we get back from our table so we can do this by using the order by statement so here I'm going to write a query that retrieves all of the records from our table where the age is less than 34 so if I run this you can see that we got four results back so now let's say that we want to order these results by the age you can see here that right now the age is out of order so if I go down here a line and type in order by now I can specify which column I want to order by so I'll type in age here and now if I run this query you can see that now the results have been sorted by the youngest to the oldest okay so that's good but what if I wanted to order by the oldest to the youngest well we can do this just by adding in this DC to the end of our our query and this stands for order by descending so if I run this now then now you can see that instead of ordering from the lowest number to the highest number now it's ordering from the highest number to the lowest number now order by ascending is the default but if you want to explicitly tell it ascending order then you can type in ASC here for ascending order now if I run this you can see that we got back the results that we did when we did the order by age so it's from the youngest to the oldest now we can also order on multiple columns so let's say that we wanted to order these results by their names so here I can just replace this with order by first name and now if I run this you can see that it did order everything by the first name but down here where we have John Smith and John Doe you can see that our John Smith record comes before John Doe but what if we wanted this to order by the entire name so so what we can do here is we can add the last name onto the end of our order by statement as well so I can put in a comma here and type in last name and now if I run this query now this is going to order all of our results first by the first name and then second by the last name so you can see here that the order of these two names is now uh in the order that we wanted we have John do before John Smith so that about does it for this video and this video we went over how to retrieve values from your database and also how we can specify certain columns and also filter the results down using the wear Clause we also looked at how what the differences were between the and and the or when used with the wear claw and we also learned how to order and sort some of our results so hopefully this video was useful for you all if you do have any questions just ask in the comment section below uh be sure to subscribe for future videos I think in the next video we'll take a look at how to update records in our database so be sure to subscribe for those future videos and thank you all for watching
Original Description
In this video we will continue learning SQL Basics by retrieving records from our database using the SELECT statement. Once we learn how to use the SELECT statement, we will learn how to filter records that match a certain criteria with the WHERE Clause. Lastly, we will learn how to sort our results using the ORDER BY statement. Let's get started.
✅ Support My Channel Through Patreon:
https://www.patreon.com/coreyms
✅ Become a Channel Member:
https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join
✅ One-Time Contribution Through PayPal:
https://goo.gl/649HFY
✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
✅ Corey's Public Amazon Wishlist
http://a.co/inIyro1
✅ Equipment I Use and Books I Recommend:
https://www.amazon.com/shop/coreyschafer
▶️ You Can Find Me On:
My Website - http://coreyms.com/
My Second Channel - https://www.youtube.com/c/coreymschafer
Facebook - https://www.facebook.com/CoreyMSchafer
Twitter - https://twitter.com/CoreyMSchafer
Instagram - https://www.instagram.com/coreymschafer/
#SQL #Postgres
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Corey Schafer · Corey Schafer · 60 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
▶
Web fonts using CSS Font Face
Corey Schafer
Using Font Awesome in Desktop Applications (OS X)
Corey Schafer
Sublime Text 2: Setup, Package Control, and Settings
Corey Schafer
ArcGIS API for JavaScript Part 1: Our First Web Map
Corey Schafer
Mac Tip: Windows' Snapping Feature on Mac with HyperDock
Corey Schafer
Linux/Mac Terminal Tutorial: Creating Aliases for Commands
Corey Schafer
ArcGIS API for JavaScript Part 2: Starting Templates
Corey Schafer
Paver Patio Time Lapse
Corey Schafer
Mac Tip: Ways to perform Screen Capturing and Screenshots
Corey Schafer
WordPress Plugins: Imsanity
Corey Schafer
WordPress Tips: Test your theme with Theme Unit Test and Monster Widget
Corey Schafer
Sublime Text 3: Setup, Package Control, and Settings
Corey Schafer
Understanding Binary, Hexadecimal, Decimal (Base-10), and more
Corey Schafer
Mac Tip: Adding Folder Stacks to the Dock
Corey Schafer
CSS Tips and Tricks: Add External URLs to Print Stylesheets
Corey Schafer
JavaScript Arrays: Properties, Methods, and Manipulation (Part 7 of 7)
Corey Schafer
JavaScript Arrays: Properties, Methods, and Manipulation (Part 1 of 7)
Corey Schafer
JavaScript Arrays: Properties, Methods, and Manipulation (Part 5 of 7)
Corey Schafer
JavaScript Arrays: Properties, Methods, and Manipulation (Part 4 of 7)
Corey Schafer
JavaScript Arrays: Properties, Methods, and Manipulation (Part 3 of 7)
Corey Schafer
JavaScript Arrays: Properties, Methods, and Manipulation (Part 2 of 7)
Corey Schafer
JavaScript Arrays: Properties, Methods, and Manipulation (Part 6 of 7)
Corey Schafer
Python Tutorial: if __name__ == '__main__'
Corey Schafer
Sublime Text Quick Tip: "Go To Definition" Click Shortcut
Corey Schafer
How to quickly create favicons for the desktop, Apple/Android devices, tablets, and more
Corey Schafer
Easily Resize Multiple Images Using Picasa
Corey Schafer
Easily Resize Multiple Images Using the Mac Terminal
Corey Schafer
Python Tutorial: virtualenv and why you should use virtual environments
Corey Schafer
Python Tutorial: pip - An in-depth look at the package management system
Corey Schafer
Git Tutorial: Using the Stash Command
Corey Schafer
How Software Engineers, Developers, and Designers can volunteer their skills
Corey Schafer
Git Tutorial: Diff and Merge Tools
Corey Schafer
Git Tutorial: Change DiffMerge Font-Size on Mac OSX
Corey Schafer
Sublime Text Quick Tip: Launch Sublime Text from the Terminal
Corey Schafer
Python Tutorial: str() vs repr()
Corey Schafer
Programming Terms: DRY (Don't Repeat Yourself)
Corey Schafer
Programming Terms: String Interpolation
Corey Schafer
Programming Terms: Idempotence
Corey Schafer
Python Tutorial: Namedtuple - When and why should you use namedtuples?
Corey Schafer
Programming Terms: Mutable vs Immutable
Corey Schafer
Python Tutorial: Else Clauses on Loops
Corey Schafer
Overview of Online Learning Resources
Corey Schafer
Mac OS X Terminal Tutorial: Time-Saving Keyboard Shortcuts
Corey Schafer
Git Tutorial for Beginners: Command-Line Fundamentals
Corey Schafer
Quickest and Easiest Way to Run a Local Web-Server
Corey Schafer
Python Tutorial: Generators - How to use them and the benefits you receive
Corey Schafer
Python Tutorial: Comprehensions - How they work and why you should be using them
Corey Schafer
Chrome Quick Tip: Quickly Bookmark Open Tabs for Later Viewing
Corey Schafer
Programming Terms: Combinations and Permutations
Corey Schafer
Git Tutorial: Difference between "add -A", "add -u", "add .", and "add *"
Corey Schafer
Preparing for a Python Interview: 10 Things You Should Know
Corey Schafer
SQL Tutorial for Beginners 1: Installing PostgreSQL and Creating Your First Database
Corey Schafer
SQL Tutorial for Beginners 2: Creating Your First Table
Corey Schafer
SQL Tutorial for Beginners 3: INSERT - Adding Records to Your Database
Corey Schafer
Linux/Mac Terminal Tutorial: Navigating your Filesystem
Corey Schafer
Python: Ex Machina Easter Egg - Hidden Message within the Code
Corey Schafer
Mac Tip: New Split Screen Feature in El Capitan
Corey Schafer
Setting up a Python Development Environment in Eclipse
Corey Schafer
Git Tutorial: Fixing Common Mistakes and Undoing Bad Commits
Corey Schafer
SQL Tutorial for Beginners 4: SELECT - Retrieving Records from Your Database
Corey Schafer
More on: SQL Analytics
View skill →Related Reads
📰
📰
📰
📰
SchemaCrawler Scribe + Google OKF: AI-Ready Database Docs You Can Keep in Git
Dev.to · Sualeh Fatehi
How to Replicate Databricks Lakebase to Snowflake with Sling
Dev.to · Fritz Larco
Export from BigQuery to PostgreSQL with Sling
Dev.to · Fritz Larco
Moving Data from BigQuery to Snowflake Using Sling
Dev.to · Fritz Larco
🎓
Tutor Explanation
DeepCamp AI