Introduction to Structured Query Language (SQL)
IN, ORDER BY, NULL and LIKE
Match names with a LIKE pattern.
IN checks whether a value is in a list: WHERE DeptId IN ('D01', 'D02', 'D04') is shorter than three conditions joined by OR. NOT IN gives the rows whose value is not in the list.
ORDER BY sorts the output by a column: ORDER BY Salary sorts ascending (the default); ORDER BY Salary DESC sorts descending, highest first.
NULL means a value is missing or unknown: it is not zero and not a blank space.
- Any arithmetic with NULL gives NULL: 5 + NULL is NULL.
- To find NULLs, use IS NULL; to find values that are present, IS NOT NULL. Writing = NULL does not work. For example, employees who have not been given a bonus: SELECT * FROM EMPLOYEE WHERE Bonus IS NULL;
LIKE matches text patterns using two wildcards:
- % stands for any number of characters (including none).
- _ (underscore) stands for exactly one character.
- 'K%': starts with K. '%a': ends with a. '%se%': contains 'se' anywhere. '_ANYA': exactly five characters ending in ANYA. '_a%': 'a' as the second letter.
Example (new): WHERE Ename LIKE 'S%' AND Ename LIKE '%a' finds names that start with S and end with a; WHERE Ename LIKE '____' (four underscores) finds names exactly four letters long.
Practise this lesson
Questions that get harder as you improve, a Tutor that helps without giving the answer away, and progress you can see. Free for every learner.
No account, no email address. Opens the real product.