Skip to main content

SQL SELECT Statement: Practical Examples for Beginners and Interview Preparation

Updated 5 min readMarkdown

The "SELECT" statement is one of the first things you learn when working with SQL. But knowing the syntax is only the beginning. In real projects, you need to retrieve the right columns, filter records, remove duplicates, sort results, and calculate useful information from tables.

Let's understand how the SQL "SELECT" statement works through practical examples using an employee table.

  1. Understanding the SELECT Statement

Suppose you have an "employees" table containing employee details.

Table: employees

id name dept salary
101 Amit IT 60000
102 Priya HR 45000
103 Rahul IT 75000
104 Neha Finance 55000
105 Sneha IT 65000

Imagine your manager asks you to prepare a list of employees working in the IT department, including their names and salaries.

You don't need every column in the table. You only need the information requested.

SQL Solution

SELECT name, salary FROM employees WHERE dept = 'IT';

Expected Output

name salary
Amit 60000
Rahul 75000
Sneha 65000

The query retrieves only the required columns and returns employees whose department is IT.

Let's look at a few more situations you may encounter while working with SQL.

  1. Retrieve All Columns from a Table

Sometimes you need to see all available columns.

SELECT * FROM employees;

The asterisk ("*") retrieves all columns from the table.

It is useful when exploring a dataset, but in production queries, explicitly listing the required columns often makes the code easier to maintain and avoids retrieving unnecessary data.

  1. Filter Records Using WHERE

Suppose you want to find employees earning more than ₹60,000.

SELECT name, salary FROM employees WHERE salary > 60000;

Expected Output

name salary
Rahul 75000
Sneha 65000

The "WHERE" clause filters rows based on a condition. You can also combine conditions using "AND" and "OR".

For example, to find IT employees earning more than ₹60,000:

SELECT name, salary FROM employees WHERE dept = 'IT' AND salary > 60000;

This returns Rahul and Sneha.

  1. Find Unique Values Using DISTINCT

Suppose you want to identify all departments represented in the employee table.

SELECT DISTINCT dept FROM employees;

Expected Output

dept

IT HR Finance

Without "DISTINCT", the IT department would appear multiple times because several employees belong to it.

Remember that "DISTINCT" applies to the combination of all selected columns, not just the first column.

  1. Sort Records Using ORDER BY

A salary report is often easier to understand when employees are listed from highest to lowest salary.

SELECT name, salary FROM employees ORDER BY salary DESC;

Expected Output

name salary
Rahul 75000
Sneha 65000
Amit 60000
Neha 55000
Priya 45000

Use "DESC" for descending order and "ASC" for ascending order.

If two employees have the same salary, you can add another sorting column to make the result order more predictable.

  1. Calculate Department-Wise Employee Counts

The "SELECT" statement also works with aggregate functions.

Suppose the HR team wants to know how many employees work in each department.

SELECT dept, COUNT(*) AS employee_count FROM employees GROUP BY dept;

Expected Output

dept employee_count
Finance 1
HR 1
IT 3

Here, "COUNT(*)" counts rows in each group, while "GROUP BY" groups employees by department.

This type of query is useful for reports, dashboards, and workforce analysis.

  1. Filter Groups Using HAVING

Now suppose you only want departments with more than one employee.

SELECT dept, COUNT() AS employee_count FROM employees GROUP BY dept HAVING COUNT() > 1;

Expected Output

dept employee_count
IT 3

The difference between "WHERE" and "HAVING" is important:

  • "WHERE" filters individual rows before grouping.
  • "HAVING" filters groups after aggregation.

This is a common topic in SQL interviews.

  1. Common SQL SELECT Interview Questions

Here are a few questions worth practising:

  1. What is the difference between "SELECT *" and selecting specific columns?
  2. How does "DISTINCT" work?
  3. What is the difference between "WHERE" and "HAVING"?
  4. How do you sort query results in descending order?
  5. Can you use aggregate functions with "GROUP BY"?
  6. How would you find the second-highest salary?
  7. What is the difference between "COUNT(*)" and "COUNT(column_name)"?
  8. How do you handle NULL values in SQL?
  9. What is the logical order of SQL query processing?
  10. How would you retrieve employees whose salaries are above the company average?

Try writing the queries yourself before looking up the answers. Understanding why a query works is more valuable than memorising its syntax.

For more questions, detailed explanations, and additional examples, you can refer to this "SQL SELECT Statement Interview Questions and Answers guide" (https://www.sankalandtech.com/Tutorials/sql-plsql-faq-interview/retrieving-data-sql-select-statement-interview-questions-faq.html).

Final Thoughts

The "SELECT" statement is much more than a way to display table data. Combined with filtering, sorting, grouping, and aggregate functions, it becomes a practical tool for solving everyday database problems.

If you're preparing for SQL interviews or working towards a data analytics or data engineering role, practise these queries on your own tables. Start with simple questions and gradually move to joins, subqueries, CTEs, and window functions.

Your turn: Which SQL concept do you find most challenging: joins, subqueries, "GROUP BY", or window functions?

Share your experience in the comments. I'd be interested to hear what you're practising.

#SQL #SQLTutorial #SQLInterviewQuestions #Database #DataAnalytics

No comments yet. Be the first to comment.