How to Selects data from a database in SQL ?
To select data from a database in SQL, you can use the SELECT statement. The basic syntax of the SELECT statement is as follows:
SELECT column1, column2, ...
FROM table_name;
Here, column1
, column2
, etc. are the names of the columns you want to retrieve data from, and table_name
is the name of the table you want to retrieve data from.
For example, if you have a table named employees
with columns id
, name
, age
, and salary
, and you want to retrieve the name
and salary
columns for all employees, you can use the following SQL query:
SELECT name, salary
FROM employees;
This will retrieve the name
and salary
columns for all employees in the employees
table.
You can also use the WHERE clause to filter the data based on certain conditions. For example, if you want to retrieve the name
and salary
columns for employees who are older than 30, you can use the following SQL query:
SELECT name, salary
FROM employees
WHERE age > 30;
This will retrieve the name
and salary
columns for all employees in the employees
table who are older than 30.
You can also use the ORDER BY clause to sort the data in ascending or descending order based on a particular column. For example, if you want to retrieve the name
and salary
columns for all employees in the employees
table, sorted in descending order of salary, you can use the following SQL query:
SELECT name, salary
FROM employees
ORDER BY salary DESC;
This will retrieve the name
and salary
columns for all employees in the employees
table, sorted in descending order of salary.