Select Query in SQL
Introduction: The SELECT statement is one of the most fundamental and frequently used queries in SQL. It is used to retrieve data from one or more tables in a database. This document provides a comprehensive overview of the SELECT query syntax and examples.
Syntax: The basic syntax of the SELECT query is as follows:
sqlSELECT column1, column2, ...
FROM table_name
WHERE condition;
Explanation:
- SELECT: The SELECT keyword is used to indicate that we want to retrieve data from the database.
- column1, column2, ...: Specify the column names you want to retrieve from the table. You can select specific columns or use "*" to select all columns.
- FROM: Specifies the table or tables from which you want to retrieve the data.
- table_name: The name of the table from which you want to retrieve data.
- WHERE: This optional clause is used to specify conditions that the retrieved data must satisfy. It filters the data based on specific criteria.
Examples:
- Select all columns from a table:
sqlSELECT * FROM employees;
- Select specific columns from a table:
sqlSELECT first_name, last_name, salary FROM employees;
- Select columns with alias names:
sqlSELECT first_name AS "First Name", last_name AS "Last Name" FROM employees;
- Select data with a condition:
sqlSELECT * FROM employees WHERE department = 'IT';
- Select data with multiple conditions:
sqlSELECT * FROM employees WHERE department = 'IT' AND salary > 50000;
- Select data with sorting:
sqlSELECT * FROM employees ORDER BY last_name ASC;
- Select data with limited rows:
sqlSELECT * FROM employees LIMIT 10;
- Select data with calculated columns:
sqlSELECT first_name, last_name, salary, salary * 0.1 AS "Bonus" FROM employees;
Conclusion: The SELECT query is a powerful tool in SQL that allows you to retrieve specific data from one or more tables in a database. By understanding its syntax and using various clauses, conditions, and functions, you can effectively retrieve and manipulate data to meet your requirements.
0 Comments