SELECT
The SELECT statement is probably the most used SQL command. The SELECT statement is used for retrieving rows from the database and enables the selection of one or many rows or columns from one or many tables in the database.
We will use the CUSTOMER table as an example.
The CUSTOMER table has the following columns:
The CUSTOMER table contains the following data:
Example:
This simple example gets all the data in the table CUSTOMER. The symbol “*” is used when you want to get all the columns in the table.
If you only want a few columns, you may specify the names of the columns you want to retrieve, example:
So in the simplest form we can use the SELECT statement as follows:
select <column_names> from <table_names>
If we want all columns, we use the symbol “*”
Note! SQL is not case sensitive. SELECT is the same as select.
The full syntax of the SELECT statement is complex, but the main clauses can be summarized as:
SELECT [ ALL | DISTINCT ] [TOP ( expression ) [PERCENT] [ WITH TIES ] ] select_list [ INTO new_table ] [ FROM table_source ] [ WHERE search_condition ] [ GROUP BY group_by_expression ] [ HAVING search_condition ] [ ORDER BY order_expression [ ASC | DESC ] ]
It seems complex, but we will take the different parts step by step in the next sections.
Select Data in the Designer Tools:
Right-click on a table and select “Select Top 1000 Rows”:
The following will appear:
A Select query is automatically created for you which you can edit if you want to.
The ORDER BY Keyword
If you want the data to appear in a specific order you need to use the “order by” keyword.
Example:
You may also sort by several columns, e.g. like this:
If you use the “order by” keyword, the default order is ascending (“asc”). If you want the order to be opposite, i.e., descending, then you need to use the “desc” keyword.