0

Table 1 Employee table

This is EMP table with 14 rows

Table 2 Dept table

This is Dept table with 4 rows

I wanted to filter employees who are working in LOC dallas and chicago

I know the correct query should be like below , we should use brackets (LOC = 'DALLAS' OR LOC = 'CHICAGO')

SELECT * 
FROM EMP E, DEPT D 
WHERE E.DEPTNO  =  D.DEPTNO 
AND (LOC = 'DALLAS' OR LOC = 'CHICAGO')

Here 11 rows were coming in output


But , i just wanted to see what would happen if we don't put brackets like below

SELECT * 
FROM EMP E, DEPT D 
WHERE E.DEPTNO  =  D.DEPTNO 
AND LOC = 'DALLAS' OR LOC = 'CHICAGO'

Here 19 rows were coming in output

I want to understand basically how this query is getting executed.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 4
    `AND` has higher precedence than `OR`, so your second query is like `WHERE (E.DEPTNO = D.DEPTNO AND LOC = 'DALLAS') OR LOC = 'CHICAGO'` – Barmar Jan 26 '23 at 21:47
  • 2
    You should stop using cross products and use ANSI JOIN to specify the relationship between the tables. – Barmar Jan 26 '23 at 21:47

0 Answers0