SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

SQL | SELECT Query

The SELECT statement is a foundational component of SQL (Structured Query Language) and is used to fetch data from a database. When you execute a SELECT statement, the result is presented as a result set, which is essentially a table of data returned by the query.

Basic Syntax

SELECT column1, column2, ...
FROM table_name
WHERE condition;

Components of the SELECT Query

  1. SELECT Clause: Lists the columns that you want to retrieve. You can also use the * wildcard to select all columns from the table.

  2. FROM Clause: Specifies the table from which you want to retrieve data.

  3. WHERE Clause: Optional. Filters the result set to include only rows where the specified condition is true.

Examples

  1. Select All Columns from a Table:

    SELECT * FROM employees;
    
  2. Select Specific Columns:

    SELECT first_name, last_name FROM employees;
    
  3. Using the WHERE Clause:

    SELECT first_name, last_name
    FROM employees
    WHERE department_id = 10;
    
  4. Ordering Results: You can use the ORDER BY clause to order the result set:

    SELECT first_name, last_name
    FROM employees
    ORDER BY last_name;
    
  5. Limiting Results: Depending on the RDBMS, you might use LIMIT, FETCH, or other clauses to limit the number of rows returned:

    • MySQL:

      SELECT first_name, last_name
      FROM employees
      LIMIT 5;
      
    • SQL Server:

      SELECT TOP 5 first_name, last_name
      FROM employees;
      
    • Oracle (using FETCH FIRST):

      SELECT first_name, last_name
      FROM employees
      FETCH FIRST 5 ROWS ONLY;
      
  6. Filtering with Multiple Conditions: You can use logical operators like AND and OR:

    SELECT first_name, last_name
    FROM employees
    WHERE department_id = 10 AND salary > 50000;
    
  7. Aggregate Functions: Use functions like SUM, AVG, COUNT, etc., to perform calculations:

    SELECT AVG(salary) AS average_salary
    FROM employees;
    

Considerations

  • Remember to always be specific about the columns you're selecting, especially when working with large databases. Using SELECT * can be resource-intensive and slow down query performance.

  • The way data is retrieved and presented can be further refined using joins, groupings, having clauses, etc., making SQL a very flexible tool for data manipulation and analysis.

  • Properly indexing your database tables can greatly improve the performance of your SELECT queries.

The SELECT statement is just the tip of the iceberg when it comes to SQL's capabilities. Combining it with other clauses and SQL constructs can create very complex and powerful queries.

  1. How to use SELECT in SQL:

    • The basic syntax of the SELECT statement to retrieve data from a table.
    SELECT column1, column2
    FROM example_table;
    
  2. Retrieving all columns with SELECT *:

    • The shorthand to select all columns from a table.
    SELECT *
    FROM example_table;
    
  3. Specifying columns in the SELECT statement:

    • Selecting specific columns from a table.
    SELECT column1, column2
    FROM example_table;
    
  4. Using DISTINCT with SELECT in SQL:

    • Removing duplicate values from the result set.
    SELECT DISTINCT column1
    FROM example_table;
    
  5. WHERE clause in SQL SELECT queries:

    • Filtering rows based on a specified condition.
    SELECT column1, column2
    FROM example_table
    WHERE column3 = 'value';
    
  6. ORDER BY clause in SQL SELECT:

    • Sorting the result set based on one or more columns.
    SELECT column1, column2
    FROM example_table
    ORDER BY column1 DESC;
    
  7. LIMIT and OFFSET with SELECT in SQL:

    • Restricting the number of rows returned and skipping a specified number of rows.
    SELECT column1, column2
    FROM example_table
    LIMIT 10 OFFSET 5;
    
  8. Filtering and sorting data with SELECT:

    • Combining WHERE for filtering and ORDER BY for sorting.
    SELECT column1, column2
    FROM example_table
    WHERE column3 = 'value'
    ORDER BY column1;
    
  9. JOIN operations with SELECT in SQL:

    • Combining data from two or more tables using JOIN operations.
    SELECT t1.column1, t2.column2
    FROM table1 t1
    JOIN table2 t2 ON t1.id = t2.id;
    
  10. GROUP BY and aggregate functions in SELECT:

    • Grouping rows based on specified columns and applying aggregate functions.
    SELECT column1, COUNT(column2) AS count_column2
    FROM example_table
    GROUP BY column1;
    
  11. HAVING clause in SELECT queries:

    • Filtering the result set after using GROUP BY and aggregate functions.
    SELECT column1, COUNT(column2) AS count_column2
    FROM example_table
    GROUP BY column1
    HAVING COUNT(column2) > 5;
    
  12. Subqueries in SELECT statements:

    • Using nested queries to retrieve data.
    SELECT column1, (SELECT MAX(column2) FROM another_table) AS max_column2
    FROM example_table;
    
  13. Using UNION and UNION ALL in SELECT:

    • Combining results from multiple SELECT statements.
    SELECT column1 FROM table1
    UNION
    SELECT column1 FROM table2;
    
  14. Advanced features of the SQL SELECT statement:

    • Advanced features may include window functions, CASE statements, and other expressions.
    SELECT column1, 
           CASE WHEN column2 > 10 THEN 'High' ELSE 'Low' END AS category,
           ROW_NUMBER() OVER (ORDER BY column1) AS row_num
    FROM example_table;