SQL Tutorial for Beginners 4: SELECT - Retrieving Records from Your Database

Corey Schafer · Beginner ·📊 Data Analytics & Business Intelligence ·10y ago

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 Web fonts using CSS Font Face
Web fonts using CSS Font Face
Corey Schafer
2 Using Font Awesome in Desktop Applications (OS X)
Using Font Awesome in Desktop Applications (OS X)
Corey Schafer
3 Sublime Text 2: Setup, Package Control, and Settings
Sublime Text 2: Setup, Package Control, and Settings
Corey Schafer
4 ArcGIS API for JavaScript Part 1: Our First Web Map
ArcGIS API for JavaScript Part 1: Our First Web Map
Corey Schafer
5 Mac Tip: Windows' Snapping Feature on Mac with HyperDock
Mac Tip: Windows' Snapping Feature on Mac with HyperDock
Corey Schafer
6 Linux/Mac Terminal Tutorial: Creating Aliases for Commands
Linux/Mac Terminal Tutorial: Creating Aliases for Commands
Corey Schafer
7 ArcGIS API for JavaScript Part 2: Starting Templates
ArcGIS API for JavaScript Part 2: Starting Templates
Corey Schafer
8 Paver Patio Time Lapse
Paver Patio Time Lapse
Corey Schafer
9 Mac Tip: Ways to perform Screen Capturing and Screenshots
Mac Tip: Ways to perform Screen Capturing and Screenshots
Corey Schafer
10 WordPress Plugins: Imsanity
WordPress Plugins: Imsanity
Corey Schafer
11 WordPress Tips: Test your theme with Theme Unit Test and Monster Widget
WordPress Tips: Test your theme with Theme Unit Test and Monster Widget
Corey Schafer
12 Sublime Text 3: Setup, Package Control, and Settings
Sublime Text 3: Setup, Package Control, and Settings
Corey Schafer
13 Understanding Binary, Hexadecimal, Decimal (Base-10), and more
Understanding Binary, Hexadecimal, Decimal (Base-10), and more
Corey Schafer
14 Mac Tip: Adding Folder Stacks to the Dock
Mac Tip: Adding Folder Stacks to the Dock
Corey Schafer
15 CSS Tips and Tricks: Add External URLs to Print Stylesheets
CSS Tips and Tricks: Add External URLs to Print Stylesheets
Corey Schafer
16 JavaScript Arrays: Properties, Methods, and Manipulation (Part 7 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 7 of 7)
Corey Schafer
17 JavaScript Arrays: Properties, Methods, and Manipulation (Part 1 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 1 of 7)
Corey Schafer
18 JavaScript Arrays: Properties, Methods, and Manipulation (Part 5 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 5 of 7)
Corey Schafer
19 JavaScript Arrays: Properties, Methods, and Manipulation (Part 4 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 4 of 7)
Corey Schafer
20 JavaScript Arrays: Properties, Methods, and Manipulation (Part 3 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 3 of 7)
Corey Schafer
21 JavaScript Arrays: Properties, Methods, and Manipulation (Part 2 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 2 of 7)
Corey Schafer
22 JavaScript Arrays: Properties, Methods, and Manipulation (Part 6 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 6 of 7)
Corey Schafer
23 Python Tutorial: if __name__ == '__main__'
Python Tutorial: if __name__ == '__main__'
Corey Schafer
24 Sublime Text Quick Tip: "Go To Definition" Click Shortcut
Sublime Text Quick Tip: "Go To Definition" Click Shortcut
Corey Schafer
25 How to quickly create favicons for the desktop, Apple/Android devices, tablets, and more
How to quickly create favicons for the desktop, Apple/Android devices, tablets, and more
Corey Schafer
26 Easily Resize Multiple Images Using Picasa
Easily Resize Multiple Images Using Picasa
Corey Schafer
27 Easily Resize Multiple Images Using the Mac Terminal
Easily Resize Multiple Images Using the Mac Terminal
Corey Schafer
28 Python Tutorial: virtualenv and why you should use virtual environments
Python Tutorial: virtualenv and why you should use virtual environments
Corey Schafer
29 Python Tutorial: pip - An in-depth look at the package management system
Python Tutorial: pip - An in-depth look at the package management system
Corey Schafer
30 Git Tutorial: Using the Stash Command
Git Tutorial: Using the Stash Command
Corey Schafer
31 How Software Engineers, Developers, and Designers can volunteer their skills
How Software Engineers, Developers, and Designers can volunteer their skills
Corey Schafer
32 Git Tutorial: Diff and Merge Tools
Git Tutorial: Diff and Merge Tools
Corey Schafer
33 Git Tutorial: Change DiffMerge Font-Size on Mac OSX
Git Tutorial: Change DiffMerge Font-Size on Mac OSX
Corey Schafer
34 Sublime Text Quick Tip: Launch Sublime Text from the Terminal
Sublime Text Quick Tip: Launch Sublime Text from the Terminal
Corey Schafer
35 Python Tutorial: str() vs repr()
Python Tutorial: str() vs repr()
Corey Schafer
36 Programming Terms: DRY (Don't Repeat Yourself)
Programming Terms: DRY (Don't Repeat Yourself)
Corey Schafer
37 Programming Terms: String Interpolation
Programming Terms: String Interpolation
Corey Schafer
38 Programming Terms: Idempotence
Programming Terms: Idempotence
Corey Schafer
39 Python Tutorial: Namedtuple - When and why should you use namedtuples?
Python Tutorial: Namedtuple - When and why should you use namedtuples?
Corey Schafer
40 Programming Terms: Mutable vs Immutable
Programming Terms: Mutable vs Immutable
Corey Schafer
41 Python Tutorial: Else Clauses on Loops
Python Tutorial: Else Clauses on Loops
Corey Schafer
42 Overview of Online Learning Resources
Overview of Online Learning Resources
Corey Schafer
43 Mac OS X Terminal Tutorial: Time-Saving Keyboard Shortcuts
Mac OS X Terminal Tutorial: Time-Saving Keyboard Shortcuts
Corey Schafer
44 Git Tutorial for Beginners: Command-Line Fundamentals
Git Tutorial for Beginners: Command-Line Fundamentals
Corey Schafer
45 Quickest and Easiest Way to Run a Local Web-Server
Quickest and Easiest Way to Run a Local Web-Server
Corey Schafer
46 Python Tutorial: Generators - How to use them and the benefits you receive
Python Tutorial: Generators - How to use them and the benefits you receive
Corey Schafer
47 Python Tutorial: Comprehensions - How they work and why you should be using them
Python Tutorial: Comprehensions - How they work and why you should be using them
Corey Schafer
48 Chrome Quick Tip: Quickly Bookmark Open Tabs for Later Viewing
Chrome Quick Tip: Quickly Bookmark Open Tabs for Later Viewing
Corey Schafer
49 Programming Terms: Combinations and Permutations
Programming Terms: Combinations and Permutations
Corey Schafer
50 Git Tutorial: Difference between "add -A", "add -u", "add .", and "add *"
Git Tutorial: Difference between "add -A", "add -u", "add .", and "add *"
Corey Schafer
51 Preparing for a Python Interview: 10 Things You Should Know
Preparing for a Python Interview: 10 Things You Should Know
Corey Schafer
52 SQL Tutorial for Beginners 1: Installing PostgreSQL and Creating Your First Database
SQL Tutorial for Beginners 1: Installing PostgreSQL and Creating Your First Database
Corey Schafer
53 SQL Tutorial for Beginners 2: Creating Your First Table
SQL Tutorial for Beginners 2: Creating Your First Table
Corey Schafer
54 SQL Tutorial for Beginners 3: INSERT - Adding Records to Your Database
SQL Tutorial for Beginners 3: INSERT - Adding Records to Your Database
Corey Schafer
55 Linux/Mac Terminal Tutorial: Navigating your Filesystem
Linux/Mac Terminal Tutorial: Navigating your Filesystem
Corey Schafer
56 Python: Ex Machina Easter Egg - Hidden Message within the Code
Python: Ex Machina Easter Egg - Hidden Message within the Code
Corey Schafer
57 Mac Tip: New Split Screen Feature in El Capitan
Mac Tip: New Split Screen Feature in El Capitan
Corey Schafer
58 Setting up a Python Development Environment in Eclipse
Setting up a Python Development Environment in Eclipse
Corey Schafer
59 Git Tutorial: Fixing Common Mistakes and Undoing Bad Commits
Git Tutorial: Fixing Common Mistakes and Undoing Bad Commits
Corey Schafer
SQL Tutorial for Beginners 4: SELECT - Retrieving Records from Your Database
SQL Tutorial for Beginners 4: SELECT - Retrieving Records from Your Database
Corey Schafer

This video teaches the basics of SQL, including how to retrieve records from a database, filter records using the WHERE Clause, and sort records using the ORDER BY statement. It covers the differences between the AND and OR operators and how to order results by multiple columns.

Key Takeaways
  1. Create a table with sample data
  2. Use the SELECT statement to retrieve all records
  3. Use the SELECT statement to retrieve specific columns
  4. Use the WHERE Clause to filter records
  5. Use the AND and OR operators to combine conditions
  6. Use the ORDER BY statement to sort records
  7. Order results by multiple columns
💡 The WHERE Clause can be used with the AND and OR operators to filter records based on multiple conditions.

Related Reads

📰
SchemaCrawler Scribe + Google OKF: AI-Ready Database Docs You Can Keep in Git
Generate AI-ready database docs with SchemaCrawler Scribe and Google OKF, and manage them in Git
Dev.to · Sualeh Fatehi
📰
How to Replicate Databricks Lakebase to Snowflake with Sling
Replicate Databricks Lakebase to Snowflake using Sling with this step-by-step guide
Dev.to · Fritz Larco
📰
Export from BigQuery to PostgreSQL with Sling
Learn to export data from BigQuery to PostgreSQL using Sling, a powerful data integration tool, to streamline your data workflow and leverage the strengths of both platforms
Dev.to · Fritz Larco
📰
Moving Data from BigQuery to Snowflake Using Sling
Learn to transfer data from BigQuery to Snowflake using Sling, a powerful data migration tool, and streamline your data warehousing process
Dev.to · Fritz Larco
Up next
How to Scrape Facebook Ad Library Data + Analyse on n8n 🔥
DroidCrunch
Watch →