SQL Foundations 02: Retrieving Data with SELECT

The SELECT statement is used to select data from a database. It's the most frequently used operation in SQL because it allows you to query the database for the information you need.

SQL Foundations 02: Retrieving Data with SELECT

Today, we're going to dive into the heart of SQL: retrieving data using the SELECT statement. It's like having a magic wand to summon exactly the data you want from your database!

Understanding SELECT
The SELECT statement is used to select data from a database. It's the most frequently used operation in SQL because it allows you to query the database for the information you need.

Step 1: Basic SELECT Usage Let's start simple. We'll use the employees table in your sql_sprint database.

  1. Open phpMyAdmin: Navigate to your phpMyAdmin interface as you did yesterday.
  2. Select the Database and Table: Click on the sql_sprint database, then click on the employees table.
  3. SQL Query: In the SQL tab, you'll see a query window. Here’s a simple query to retrieve all data from the employees table: SELECT * FROM employees; The asterisk (*) is a wildcard that means “all columns.”
  4. Run the Query: Press the ‘Go’ button to execute your query. You'll see the entire dataset from the employees table displayed.
Entering the SQL query
The results of running the query

Step 2: Selecting Specific Columns Now, let’s say you only want to see the names and email addresses of the employees. Modify your query like this:

SELECT first_name, last_name, email FROM employees;

Run this query. Notice how the results now only include the columns you specified.

Results with only specified columns

Step 3: Understanding Results Take a moment to explore the output. How many records are there? Do you notice any patterns or interesting data points?

Encountered a Hitch? If you're having trouble with your queries, don’t hesitate to reach out on our ​Slack channel​. We're here to help you through each step.

Your Mission: Experiment with the SELECT statement. Try selecting different columns or even a single column from the employees table. What happens if you only select department?

Final Thought: Today, you’ve wielded the power of SELECT, the fundamental tool in your SQL toolkit. Each query is a step towards mastering data retrieval!