Basic SELECT Queries
Welcome to the world of SQL! Today, we’ll start learning how to use the basic SELECT query. SELECT is one of the most important commands in SQL because it allows you to retrieve or “select” data from a database. Think of a database like a giant bookshelf full of books, and each book contains information. The SELECT query is like the librarian helping you find and read specific books.
Let’s dive into this simple but powerful SQL command.
What is the SELECT Query?
The SELECT
query allows you to pick certain pieces of information from a table in a database. It’s like asking the librarian, “Can you give me all the books with the title ‘Harry Potter’?”
Here’s a basic structure of the SELECT query:
SELECT column1, column2, ...
FROM table_name;
SELECT
: This tells SQL that you want to retrieve data.column1, column2, ...
: These are the names of the columns you want to see.FROM
: This tells SQL from which table you want to get the data.table_name
: This is the name of the table where the data is stored.
Basic SELECT Query Example
Imagine we have a table called students
that looks like this:
student_id | first_name | last_name | age | grade |
---|---|---|---|---|
1 | John | Doe | 15 | A |
2 | Jane | Smith | 16 | B |
3 | Jim | Brown | 14 | A |
Query Example 1: Select all columns from the students
table.
SELECT *
FROM students;
*
: The asterisk (*) is a shortcut that means “all columns”.- This query will return all the columns for every student in the table.
The result will look like this:
student_id | first_name | last_name | age | grade |
---|---|---|---|---|
1 | John | Doe | 15 | A |
2 | Jane | Smith | 16 | B |
3 | Jim | Brown | 14 | A |
Query Example 2: Select specific columns.
SELECT first_name, last_name
FROM students;
- Here, we only want to see the
first_name
andlast_name
columns. - The result will be:
first_name | last_name |
---|---|
John | Doe |
Jane | Smith |
Jim | Brown |
Filtering Data with WHERE Clause
Sometimes, you only want to see data that meets certain conditions. For example, you might want to find only the students who are in grade “A”. You can use the WHERE
clause to filter the data.
Query Example 3: Select students with grade ‘A’.
SELECT first_name, last_name, grade
FROM students
WHERE grade = 'A';
WHERE
: This keyword filters the data based on the condition you provide.- The result will be:
first_name | last_name | grade |
---|---|---|
John | Doe | A |
Jim | Brown | A |
Sorting Data with ORDER BY
You can also sort the results to make them easier to read. For example, if you want the students to be listed from the youngest to the oldest, you can use the ORDER BY
clause.
Query Example 4: Select students and sort them by age.
SELECT first_name, last_name, age
FROM students
ORDER BY age;
ORDER BY
: This keyword sorts the data based on the column you choose.- By default,
ORDER BY
sorts in ascending order (from smallest to largest). - The result will be:
first_name | last_name | age |
---|---|---|
Jim | Brown | 14 |
John | Doe | 15 |
Jane | Smith | 16 |
Query Example 5: Select students and sort them by age in descending order.
SELECT first_name, last_name, age
FROM students
ORDER BY age DESC;
DESC
: This sorts the data in descending order (from largest to smallest).- The result will be:
first_name | last_name | age |
---|---|---|
Jane | Smith | 16 |
John | Doe | 15 |
Jim | Brown | 14 |
Practice Questions
Now, let’s practice writing some SELECT queries. Try to solve these by yourself, and then we’ll check the solutions.
- Get all the information about the students in the
students
table. - Select only the
first_name
andage
of students who are 15 years old or older. - List the students sorted by their
last_name
. - Get all students who have a grade “B”.
- Select all columns and sort the students by their
grade
in alphabetical order.
Solutions to Practice Questions
- Get all the information about the students in the
students
table.
SELECT *
FROM students;
- Select only the
first_name
andage
of students who are 15 years old or older.
SELECT first_name, age
FROM students
WHERE age >= 15;
- List the students sorted by their
last_name
.
SELECT first_name, last_name
FROM students
ORDER BY last_name;
- Get all students who have a grade “B”.
SELECT *
FROM students
WHERE grade = 'B';
- Select all columns and sort the students by their
grade
in alphabetical order.
SELECT *
FROM students
ORDER BY grade;
Interview Questions
These questions could be asked in an SQL interview to test your understanding of basic SELECT queries.
- What does the SELECT query do in SQL?
- The SELECT query is used to retrieve data from a database table.
- How do you select all columns from a table?
- Use the
SELECT *
query. The asterisk (*) represents all columns.
- Use the
- How do you filter data in a SELECT query?
- Use the
WHERE
clause to specify conditions that the data must meet.
- Use the
- How do you sort data in a SELECT query?
- Use the
ORDER BY
clause to sort the data by a specific column.
- Use the
- What is the difference between
ASC
andDESC
in anORDER BY
clause?ASC
sorts in ascending order (smallest to largest), whileDESC
sorts in descending order (largest to smallest).
We’ve covered the basics of the SELECT query, which helps you retrieve data from a database. You learned how to:
- Select all or specific columns.
- Filter data using the
WHERE
clause. - Sort data using the
ORDER BY
clause.
With these tools, you can start writing simple queries to interact with databases. Keep practicing to get better at it!