SELECT Statement in SQL

The SELECT statement is used to query the database and retrieve selected data that match the criteria that you specify.
The SELECT statement has five main clauses to choose from, although, FROM is the only required clause. Each of the clauses have a vast selection of options, parameters, etc. The clauses will be listed below, but each of them will be covered in more detail later in the tutorial.
Here is the format of the SELECT statement:
SELECT [ALL | DISTINCT] column1[,column2]
FROM table1[,table2]
[WHERE “conditions”]
[GROUP BY “column-list”]
[HAVING “conditions]
[ORDER BY “column-list” [ASC | DESC] ]

SELECT column_name(s)
FROM table_name

SELECT * FROM table_name
The DISTINCT clause allows you to remove duplicates from the result set. The DISTINCT clause can only be used with select statements.
The syntax for the DISTINCT clause is:
SELECT DISTINCT columns     FROM tables     WHERE predicates;
Example #1
Let’s take a look at a very simple example.
    FROM suppliers;
This SQL statement would return all unique cities from the suppliers table.
Example #2
The DISTINCT clause can be used with more than one field.
For example:
   SELECT DISTINCT city, state
    FROM suppliers;
This select statement would return each unique city and state combination. In this case, the distinct applies to each field listed after the DISTINCT keyword.
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply