Go to: Articles List

SQL Basics Part 7 : And, Or & Order By

     Using the And keyword you can increase or narrow the range of your search:

SELECT * FROM CustomerTable WHERE lname LIKE 'S%' AND fname = 'Bill'

The above query would give you all records where lname starts with 'S' and that have a fname equal to 'Bill'.

     Using the Or keyword you can also increase or narrow the range of your search:

SELECT * FROM CustomerTable WHERE lname LIKE 'S%' OR lname LIKE 'B%'

This query would give you all records where lname starts with 'S' OR 'B'.

     Using the Order By keywords you can order your records output by a certain column. You can order by ascending or descending. Ascending ordering is the default.

Syntax:

SELECT * FROM TableName ORDER BY ColumnName DESC

The above code orders the results by ColumnName in descending order. To specify the order by ascending order replace 'DESC' with 'ASC'. You can also order by more than one column name. To do this simply specify the additional column names after the first one separated by a comma.