Understanding logical expressions in sql?
Answers:
There are effectively 3 logical expressions in SQL. These are :-
AND, OR , NOT. When interrogating a database you need to specifiy the table where you want to take your information from. You also need to include a condition i.e. you just want a subset of the overall data.
For example: - SELECT EMPLOYEES_NAME
FROM EMPLOYEES <this is your table>
WHERE EMPLOYEE_SEX = 'MALE' <condition>
But you may also want males who were born in England. This is where you logical expression comes in. You now have 2 conditions which you combine with the logical expression 'AND'.
For example: - SELECT EMPLOYEES_NAME
FROM EMPLOYEES <this is your table>
WHERE EMPLOYEE_SEX = 'MALE' <condition>
AND EMPLOYEE_COUNTRY = 'ENGLAND'
So you get males from England. This also applies to 'OR'. I want male employees from England or Scotland.
For example:- SELECT EMPLOYEES_NAME
FROM EMPLOYEES <this is your table>
WHERE EMPLOYEE_SEX = 'MALE' <condition>
( AND EMPLOYEE_COUNTRY = 'ENGLAND'
OR EMPLOYEE_COUNTRY = 'SCOTLAND')
The brackets are used to indicate that you want males but they must be from England OR Scotland.
A similar logic applies to the statement 'NOT' You may require male employees who are not form England. So:-
SELECT EMPLOYEES_NAME
FROM EMPLOYEES <this is your table>
WHERE EMPLOYEE_SEX = 'MALE' <condition>
AND EMPLOYEE_COUNTRY != 'ENGLAND'
By the way != is the same as not equal to.
what
The answers post by the user, for information only, UKQnA.com does not guarantee the right.