Mysql Select command
The SELECT command in MySQL is used to retrieve data from one or more tables in a database. The syntax for the SELECT statement is as follows:
SELECT column1, column2, ..., columnN
ORDER BY column_name [ASC|DESC]
LIMIT num_rows OFFSET offset_value;
SELECT statement retrieves one or more columns from a table in a database. The columns to be retrieved are specified by a comma-separated list of column names. To retrieve all columns from a table, use an asterisk (*) in place of the column names.
FROM clause specifies the name of the table from which the data is to be retrieved.
WHERE clause is optional and specifies a condition that must be satisfied for a row to be retrieved. The condition can include operators such as
ORDER BY clause is used to sort the result set in ascending or descending order based on one or more columns. The default sorting order is ascending, but you can specify descending order by adding the
DESC keyword after the column name.
LIMIT clause is used to limit the number of rows returned by the query. It takes two arguments:
num_rows, which specifies the maximum number of rows to return, and
offset_value, which specifies the number of rows to skip before starting to return rows.
To retrieve all columns from a table named
To retrieve the
last_name columns from the
employees table where the
department column is equal to ‘Sales’ and the
salary column is greater than 50000:
SELECT first_name, last_name
WHERE department = 'Sales' AND salary > 50000;
To retrieve the
unit_price columns from the
products table sorted in descending order by
SELECT product_name, unit_price
ORDER BY unit_price DESC;
To retrieve the first 10 rows from the
To retrieve the next 10 rows from the
orders table after skipping the first 10 rows:
LIMIT 10 OFFSET 10;
These are some of the basic examples of using the
SELECT command in MySQL to retrieve data from one or more tables in a database. The
SELECT command can be combined with other commands such as
GROUP BY, and
HAVING to perform more complex queries.