Interview Questions, Answers and Tutorials

Basic SELECT Queries

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_idfirst_namelast_nameagegrade
1JohnDoe15A
2JaneSmith16B
3JimBrown14A

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_idfirst_namelast_nameagegrade
1JohnDoe15A
2JaneSmith16B
3JimBrown14A

Query Example 2: Select specific columns.
SELECT first_name, last_name
FROM students;




  • Here, we only want to see the first_name and last_name columns.
  • The result will be:
first_namelast_name
JohnDoe
JaneSmith
JimBrown

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_namelast_namegrade
JohnDoeA
JimBrownA

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_namelast_nameage
JimBrown14
JohnDoe15
JaneSmith16

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_namelast_nameage
JaneSmith16
JohnDoe15
JimBrown14

Practice Questions

Now, let’s practice writing some SELECT queries. Try to solve these by yourself, and then we’ll check the solutions.

  1. Get all the information about the students in the students table.
  2. Select only the first_name and age of students who are 15 years old or older.
  3. List the students sorted by their last_name.
  4. Get all students who have a grade “B”.
  5. Select all columns and sort the students by their grade in alphabetical order.

Solutions to Practice Questions

  1. Get all the information about the students in the students table.
SELECT * 
FROM students;




  1. Select only the first_name and age of students who are 15 years old or older.
SELECT first_name, age
FROM students
WHERE age >= 15;




  1. List the students sorted by their last_name.
SELECT first_name, last_name
FROM students
ORDER BY last_name;




  1. Get all students who have a grade “B”.
SELECT * 
FROM students
WHERE grade = 'B';




  1. 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.

  1. What does the SELECT query do in SQL?
    • The SELECT query is used to retrieve data from a database table.
  2. How do you select all columns from a table?
    • Use the SELECT * query. The asterisk (*) represents all columns.
  3. How do you filter data in a SELECT query?
    • Use the WHERE clause to specify conditions that the data must meet.
  4. How do you sort data in a SELECT query?
    • Use the ORDER BY clause to sort the data by a specific column.
  5. What is the difference between ASC and DESC in an ORDER BY clause?
    • ASC sorts in ascending order (smallest to largest), while DESC 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!