Languages

SQL Basics, SELECT, Ordering

Database constists of tables. Table is collections of rows, every row has one or more columns. Below is Genre table from Chinook sample database:

GenreId Name
1 Rock
2 Jazz
3 Metal
4 Alternative & Punk
5 Rock And Roll
6 Blues
7 Latin

SELECT commad

With SQL, you can select all rows from table Genre using following command:

SELECT * FROM Genre

SELECT command reads data from database. "*" denotes that all columns should be read. After FROM, source table follows. The same result you will obtain with command

SELECT GenreId, Name FROM Genre

Explore tables using DatAdmin

In "Connection tree", you could see list of tables. If you select table in connection tree, you can see table structure in "Summary" tab.

DatAdmin - table dashboard

SELECT DISTINCT

SELECT statement has DISTINCT option, which causes that only unique rows are returned.

If you want to see all cities referenced from "Customer" table, you could use query:

SELECT DISTINCT City FROM Customer

Ordering Result

You can specify order of result rows using ORDER BY clause. You could use option DESC for descending order. Opposite option is ASC (ascending), which can be as default value omited.

Syntax is:

SELECT columns FROM table ORDER BY column1 [DESC] [,column2 [DESC]]

Examples

SELECT * FROM Customer ORDER BY Country
SELECT * FROM Customer ORDER BY Country DESC, City ASC

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options