Database Basic SQL Queries Filtering Data Working with Functions Joining Tables Subqueries Modifying Data Advanced Queries Transactions and Locking Indexing and Optimization SQL Best Practices

SELECT Statement

SELECT statement is used to select data from a database. The data returned is stored in a result table, called the result-set.

Let's take an example for the given STUDENTS table.

StudentID FirstName LastName City
1 John Doe Seattle
2 Jane Smith San Francisco
3 Tom Johnson Los Angeles

SELECT FirstName,LastName

FROM STUDENTS;

FirstName LastName
John Doe
Jane Smith
Tom Johnson

WHERE Clause

WHERE clause is used to filter records. The WHERE clause is used to extract only those records that fulfill a specified condition.

Let's take an example for the given STUDENTS table.

StudentID FirstName LastName City
1 John Doe Seattle
2 Jane Smith San Francisco
3 Tom Johnson Los Angeles

SELECT FirstName,LastName

FROM STUDENTS

WHERE City='Seattle';

FirstName LastName
John Doe

ORDER BY Clause

ORDER BY clause is used to arrange the records. The ORDER BY clause can be used to arrange the records on the basis of ascending, descending order.

Let's take an example for the given STUDENTS table.

StudentID FirstName LastName City
1 John Doe Seattle
2 Jane Smith San Francisco
3 Tom Johnson Los Angeles

SELECT FirstName,LastName

FROM STUDENTS

ORDER BY FirstName;

FirstName LastName
Jane Smith
John Doe
Tom Johnson

LIMIT and OFFSET Clauses

LIMIT and OFFSET clauses are used to restrict the number of records returned by a query. LIMIT clause is used to limit the number of records to return, and OFFSET clause is used to skip a specified number of records before beginning to return records.

Let's take an example for the given STUDENTS table.

StudentID FirstName LastName City
1 John Doe Seattle
2 Jane Smith San Francisco
3 Tom Johnson Los Angeles

SELECT FirstName,LastName

FROM STUDENTS

LIMIT 2 OFFSET 1;

FirstName LastName
Jane Smith
Tom Johnson