Wednesday 25 September 2013

Using Minus query in SQL.

(select * from employee_salary)
minus
(select * from employee_salary where to_date(pay_month_year,'mm/rrrr')<='01-dec-2012)

Just look into the above query.

Sql in the first line returns all the records.
Now i apply minus with the secong query which returns all the records where pay_month_year is less
than equal to december-2012. So the result will be all the records where pay_month_year starting from january-2013. (please note that pay_month_year is in the format mm/rrrr i.e. 12/2012.

Lets take another example.

(select * from employee_salary)
minus
(select * from employee_salary where gross_pay>=20000)

second query returns all the records whose gross_pay is greater than 20000, and first query returns
all the records so result of the query will return all the records where gross_pay is less than 20000.

No comments: