SQL Order By, Where Clause, Like Operator and SQL Wildcards

 In this post we will discuss about:

  • SQL Order By
  • Where Clause
  • SQL Like
  • SQL Wildcards
  • Problems and write SQL queries

Order By 

Order By - The ORDER BY keyword is used to sort the result-set in ascending or descending order. The ORDER BY keyword sorts the records in ascending order by default.

Where Clause

The WHERE clause is used to filter records. It is used to extract only those records that fulfill a specified condition. SQL requires single quotes around text values. However, numeric fields should not be enclosed in quotes.
The following operators can be used in the WHERE clause: =, >, <, >=, <=, <>, BETWEEN, LIKE, IN 

SQL Wildcards

A wildcard character is used to substitute one or more characters in a string.
Wildcard characters are used with the LIKE operator. 

SQL Like 

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
%  - Represents zero or more characters
Example: Hi% finds Hitendra, Hit, Hi
_   -Represents a single character

Example: V_rma find Verma, varma

[] - Represents any single character within the brackets

Example: h[oa]t finds hot and hat, but not hit
^  - Represents any character not in the brackets

h[^oa]t finds hit, but not hot and hat
- Represents a range of characters

Example: c[a-b]t finds cat and cbt

Queries to Solve
  1. List the employees in the asc order of their Salaries? 
  2. List the details of the emps in asc order of the FirstName and desc of Department? 
  3. Select employee detail whose name is "Jane" 
  4. Get all employee detail from Employee table whose "FirstName" start with letter 'r'. 
  5. Get all employee details from Employee table whose "FirstName" contains 'a' 
  6. Get all employee details from EmployeeDetail table whose "FirstName" end with 'm' 
  7. Get all employee detail from EmployeeDetail table whose "FirstName" start with any single character between 'a-p' 

No comments:

Post a Comment