SQL WHERE Clause: Repetitions & Order Explained

  • Comp Sci
  • Thread starter Crystal037
  • Start date
  • Tags
    Sql
In summary, the problem is that the query is trying to match every row in the first table to every row in the second table, and it's getting redundant data because of the cartesian join. You can fix the problem by rewriting the query to use an appropriate join syntax.
  • #1
Crystal037
167
7
Homework Statement
I am trying to understand how sql query selects multiple times the same value when I use a combination of condition in where.
I have 4 tables employee, project,department,works_on.
I have to find all project number that involve an employee with last name='Wong' either as a worker or as a manager of the department that controls the project.
Relevant Equations
select pnumber
from project,works_on,employee,department
where lname='Wong'
and ((dnum=dnumber and mgr_ssn=ssn) or (essn=ssn and pnumber=pno));
I expect output as 30,20,3,2,1 in a row but I am getting repetitions of these with 20,30 coming 3 times and 3,2 coming 18 times and 1 coming 16 times. I dont understand how combination of condition in where is leading to so many duplicates. Instead of using or if I divide the query into 2 and execute it one by one then I am getting only distinct values. Can you also explain the order by which it traverses through the table and selects tuples and display as to understand why am I getting those multiple values and why in that specific order
Screenshot (136).png
Screenshot (137).png
Screenshot (138).png
Screenshot (139).png
Screenshot (140).png
 
Physics news on Phys.org
  • #2
Repitition like that is usually due to a cartesian join, where some part of the join is not defined. So each row of one table is joined to EVERY row of another table. And I think that the OR is one of the things doing that. So read up on cartesian joins to see where it is going wrong.

If your select was on more than just Pnumber, say include Pno from works_on, or Pno from works_on and Dnumber from department you should see groupings of data showing how the cartesian join is collecting repetively data on a table by table basis.

It always helps to recover date where you specify the tables in the select's join rather than just a column name. You have not mentioned which table's dnum equals which table's dnumber, for example.
 
  • Like
Likes Crystal037 and jim mcnamara
  • #3
The query optimiser will test the number of rows it gets in lots of different joins and then use that to work out which join to do first, then collect data from that and choose which one it does next. This depends on indexes existing as well, if I remember correctly.
 
  • #4
You could probably replace your first line with "select distinct pnumber" and get the result you expect. But that would be a bad fix.

The real fix you need is to rewrite your query with an appropriate JOIN syntax to eliminate the problem explained by @DrJohn . Here are links that explain how and why you should:
 
  • Like
Likes jim mcnamara
  • #5
If you join on dnum = dnumber you get nr 5 times 9, since dnumber is 5 thrice and dnum as well. This all goes for the same person, so the join isn't uniquely linked.
 
  • #6
Joining with comma's or with the equivalent cross join takes every combination of rows once. If you then specify dnum = dnumber the amount of rows where both equal 5 becomes 9 since for both tables there are 3 such rows. But you also join on pnum = pnumber and these match on 2 and 3 where again dnum = dnumber = 5. Therefore these occurences (2 and 3) are again multiplied by 2, yielding 18 times 2 and 3.

BTW this also occurs on an inner join on the same conditions, you need to take a join condition that uniquely identifies one correct row.
 
Last edited:

FAQ: SQL WHERE Clause: Repetitions & Order Explained

What is the purpose of the SQL WHERE clause?

The SQL WHERE clause is used to filter records in a database query. It specifies the conditions that must be met for the rows to be included in the result set. This allows you to retrieve only the data that meets certain criteria, making your queries more precise and efficient.

Can you use multiple conditions in a SQL WHERE clause?

Yes, you can use multiple conditions in a SQL WHERE clause by combining them with logical operators such as AND, OR, and NOT. For example, you can filter records that meet both condition A and condition B, or records that meet either condition A or condition B.

How does the order of conditions in a SQL WHERE clause affect the query?

The order of conditions in a SQL WHERE clause can affect the query's performance but not the final result. SQL engines optimize the query execution plan, but placing the most restrictive conditions first can sometimes lead to faster query execution by reducing the number of rows processed early on.

What happens if a condition in the WHERE clause is not met?

If a condition in the WHERE clause is not met, the corresponding row is excluded from the result set. The WHERE clause acts as a filter, and only the rows that satisfy all specified conditions are returned in the query results.

Can you use subqueries in a SQL WHERE clause?

Yes, you can use subqueries in a SQL WHERE clause. A subquery is a nested query that provides a result set to be used by the main query. Subqueries can be used to perform more complex filtering by comparing a column to the result of another query.

Similar threads

Replies
17
Views
1K
Replies
7
Views
2K
Replies
2
Views
1K
Replies
2
Views
4K
Replies
4
Views
2K
Back
Top