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
- List the employees in the asc order of their Salaries?
Answer: Select * from EmployeeDetail Order By Salary;
- List the employees in the desc order of their Salaries?
Answer: Select * from EmployeeDetail Order By Salary DESC;
- List the details of the emps in asc order of the FirstName and desc of Department?
Answer: SELECT * from EmployeeDetail Order BY Firstname ASC, Department DESC;
- Select employee detail whose name is "Jane"
Answer: Select * from EmployeeDetail where FirstName='Jane'
- Get all employee detail from Employee table whose "FirstName" start with letter 'r'.
Answer: Select * from EmployeeDetail where FirstName like 'R%'
- Get all employee details from Employee table whose "FirstName" contains 'a'
Answer: Select * from EmployeeDetail where FirstName like '%a%'
- Get all employee details from EmployeeDetail table whose "FirstName" end with 'm'
Answer: Select * from EmployeeDetail where FirstName like '%m'
- Get all employee detail from EmployeeDetail table whose "FirstName" start with any single character between 'a-p'
Answer: Select * from EmployeeDetail where FirstName like '[a-p]%'
Please refer below YouTube video for more detail explanation:
No comments:
Post a Comment