0 of 3 saved

Database Management Systems2 min read

SELECT query

Read this chapter, write the program on this page, then pass a short check to open the next one.

In this tutorial we will learn how to use SELECT SQL query. SELECT query is a command which is used to retrieve data from tables. This is the most used SQL query.

SELECT SQL Query

SQL SELECT Query is a Data Query Language (DQL) in SQL. SELECT query is used to retrieve data from a table. It is the most commonly used SQL query. We can retrieve complete table data, or partial by specifying conditions using the WHERE clause.

Syntax:

  SELECT column_name1, column_name2, ...,column_name_n
  FROM table_name;

Let us consider a table 'employee' with the following five fields and four records.

sql table

Example: Select Records using Column Names

The SELECT query is used to retrieve records from a table. We can specify the names of the columns which we want in the resultset. The below SQL query will fetch information of id, name, and age columns of the employee table and display them. Observe the output, the data from the location and salary column is absent because we did not specify it in our SELECT query.

  SELECT id, name, age FROM employee;

Output:

sql table

Example: Select All Records from a Table

A special character asterisk * is used to address all the data (belonging to all columns) in a query. The SELECT statement uses * character to retrieve all records from a table, for all the columns. The below SQL query will show all the records of the employee table, which means it will show the complete dataset of the table.

  SELECT * FROM employee;

Output:

sql table

Example: Select a Particular Record based on a Condition

In this example, we use the WHERE clause to set a condition.

  SELECT * FROM employee 
  WHERE salary = 3000;

Output:

sql table

Example: Perform Simple Calculations

The below SQL command will display a new column in the result, with 3000 added to the existing salaries of the employees.

  SELECT id, name, salary+3000 
  FROM employee;

sql table

Conclusion

In this article, we discussed different examples of SQL SELECT query. You can use any type of SQL clauses like GROUP BY, WHERE, HAVING, ORDER BY, etc, with a SELECT query.