Basic SELECT Statement

SQL Data Querying (DQL - Data Query Language)

Data Query Language (DQL) is a subset of SQL that focuses on querying and retrieving data from databases. The most fundamental command in DQL is the SELECT statement, which allows you to extract specific data from one or more tables. In this section, we will cover the basics of the SELECT statement, including how to retrieve data using SELECT * and how to specify particular columns.


Basic SELECT Statement

The SELECT statement is the primary tool for querying data from a database. It allows you to specify which columns you want to retrieve and from which table. You can also filter and sort the data, but for now, we'll focus on the basic usage.

1. Retrieving Data from a Table

To retrieve data from a table, you use the SELECT statement followed by the names of the columns you want to retrieve and the name of the table.

Syntax:

SELECT column1, column2, ...
FROM table_name;
  • column1, column2, ...: The names of the columns you want to retrieve.

  • table_name: The name of the table from which you want to retrieve data.

2. Using SELECT * to Retrieve All Columns

If you want to retrieve all the columns from a table, you can use the * wildcard. This will return all the data stored in the table.

Example:

SELECT * 
FROM employees;

In this example:

  • The SELECT * statement retrieves all columns from the employees table. This is helpful when you want to see all the data in the table without specifying individual columns.

Result:

  • The result displays all columns from the employees table.

3. Retrieving Specific Columns

If you only need certain columns from a table, you can specify the columns you want to retrieve by listing them after the SELECT keyword.

Example:

SELECT first_name, last_name, salary 
FROM employees;

In this example:

  • The SELECT statement retrieves only the first_name, last_name, and salary columns from the employees table.

Result:

  • The result displays only the first_name, last_name, and salary columns from the employees table.


Conclusion

The SELECT statement is the foundation of SQL data querying, allowing you to retrieve data from one or more tables in a database. Whether you need to retrieve all columns using SELECT * or just specific columns, mastering the basics of the SELECT statement is essential for working with SQL databases.

This tutorial is brought to you by codes with pankaj.

Last updated