SELECT
The SELECT
statement is used in SQL to pick out specific data from a database. In other words, it is used to select from the database what you would like to display. The syntax for the SELECT
statement is fairly straightforward:
SELECT column(s)
FROM table
WHERE condition;
column(s)
: Enter the name(s) of the column(s) that you want to display.table
: The name of the table from where you want to retrieve data.WHERE
: Optional. This is a filter to display only the rows where this condition is true.
For instance, if you wanted to select all data from the "Customers" table, your query would look like this:
SELECT *
FROM Customers;
In the above code, the asterisk *
denotes "all". This will retrieve all of the data in the "Customers" table.
If you want to select only the "FirstName" and "LastName" columns from the "Customers" table, your query would look like this:
SELECT FirstName, LastName
FROM Customers;
You can also filter using the WHERE
clause. For example, selecting only the customers who are from "Germany":
SELECT *
FROM Customers
WHERE Country='Germany';
Finally, you can also sort the results using the ORDER BY
keyword:
SELECT *
FROM Customers
ORDER BY Country;
This will sort the output in ascending order by the Country column. To sort in descending order, you would add the DESC
keyword:
SELECT *
FROM Customers
ORDER BY Country DESC;
These are the very basics of the SELECT
statement in SQL, which is a vital part of working with databases.