The SELECT statement in MySQL is used to retrieve data from one or more tables.
SELECT statement allows you to specify which columns you want to retrieve from Tables.
It provides operators to perform operations while retrieving data from a table such as filtering, sorting, grouping, and limiting operations.
SELECT columnName1, columnName2 FROM tableName WHERE condition;
columnName1, columnName1: The columns you want to retrieve from the table. We can also use an asterisk (*) to select all columns.
tableName: The name of the table from which we can retrieve data.
WHERE condition: An optional clause that allows us to filter the results based on a specified condition.
`*` syntax indicates that we want to retrieve all columns from the table.
SELECT * FROM products;
Defining names in the `SELECT` statement indicates that we want to retrieve these columns from tables. for ex:- in the below query we have specified `product_name`, `product_price`, and `category`.
SELECT product_name, product_price, category FROM products;
Defining the `WHERE` clause in the `SELECT` statement indicates that we want to filter out data based on the Column from the table. for ex:- in the below query we have specified `category = GAMES`. so it will retrieve all records from the table where a `category` is equal to `GAMES`.
SELECT * FROM products WHERE category = 'GAMES';
Defining the `DISTINCT` Operator in the `SELECT` statement indicates that we want to retrieve unique records based on specific Columns from the Table.
SELECT DISTINCT category FROM products;
The `LIMIT` Operator applies a limit on Query to return a Specific Number of Records from the table.
SELECT * FROM products LIMIT 20;
The above query will return 20 records from the `products` table at once.