One variant of question asked SQL: IF clause within WHERE clause
Let's say :DeptID
is a parameter passed to the SQL query and if @DeptID
is null, then show all rows, else filter the result by DeptID
.
Some pseudo code like
SELECT *
FROM EMPLOYEE
IF :DeptID not = null
WHERE DeptID = :DeptID
ELSE (no filter)
Looking for correct syntax suitable for PostgreSQL & MySQL
The expected result is: when the parameter DeptID
passed in is NULL
, then show all employees.
And when the parameter DeptID
is passed as 10, show employees for the department with an ID of 10.