Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

SQL Between operator

SQL Between

The BETWEEN operator is used to select values within a range in a SELECT, INSERT, UPDATE, or DELETE statement.

Note:

 1. BETWEEN operators return records where expression is within the range of value1 and value2 (inclusive).
 2. The between values can be numbers, text, or dates.

Syntax:

SELECT column_1, column_2..column_n FROM tableName
WHERE some_column BETWEEN value1 AND value2;


Example:

SELECT  * FROM Categories WHERE CategoryID BETWEEN 1 and 5

SQL Select Top

The SELECT TOP clause is used to specify the number of records to return from result set.

Top clause is useful if table contains large amount of data.

Note: All RDBMS Datbases doesn't support top (example ORACLE,MySql doesn't support top but MS-SQL supports TOP)

Syntax:

SELECT TOP number|percent  column 1, column 2..column n FROM tableName;

 

Example:

 SELECT TOP 2 CategoryName,[Description] FROM Categories

Result:


SQL Order by Clause

SQL Order by:

The SQL ORDER BY clause is used to sort the result set in ascending or descending order, based on one or more columns.

For some RDBMS database default order is ascending, to sort the records in a descending order, you can use the DESC keyword.

Syntax:

SELECT column_1, column_2..column_n
FROM table_name ORDER BY some_column ASC|DESC, column_name ASC|DESC;


Example:

On one column:

Ascending:

SELECT * FROM Categories ORDER BY CategoryName
FROM Categories ORDER BY CategoryName

Descending:
SELECT *
FROM Categories ORDER
BY CategoryName DESC

On two columns:

Ascending:
SELECT * FROM Categories ORDER BY CategoryName,[Description]
Descending:
SELECT * FROM Categories ORDER BY CategoryName,[Description] DESC