Some More SQL Queries on Wildcard, Distinct etc

  •  Get all employee detail from Employee table whose "FirstName" not start with any single character between 'a-p' 
Answer:  SELECT * FROM [Employee] WHERE FirstName like '[^a-p]%'
  • Get all employee detail from Employee table whose "Gender" end with 'le' and contain 4 letters. 

Answer:  --The Underscore(_) Wildcard Character represents any single character. 

SELECT * FROM [EmployeeDetail] WHERE Gender like '__le' --there are two "_"

  • Get all employee detail from Employee table whose "FirstName" start with 'F' and contain 5 letters. 
Answer: SELECT * FROM [EmployeeDetail] WHERE FirstName like 'F____' --there are four “_”
  • Get all employee detail from Employee table whose "FirstName" containing '%'. ex:-"Fra%nk".
Answer: SELECT * FROM [EmployeeDetail] WHERE FirstName like '%[%]%' 
--According to our table it would return 0 rows, because no name containg '%

DISTINCT keyword - The SELECT DISTINCT statement is used to return only distinct (different) values.
The SQL DISTINCT keyword is used in conjunction with the SELECT statement to eliminate all the duplicate records and fetching only unique records.

  • Get all unique "Department" from Employee table. 

Answer: SELECT DISTINCT(Department) FROM EmployeeDetail

Please refer below YouTube video for more detail explanation:

No comments:

Post a Comment