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:

1. What is the purpose of the SQL WHERE clause?

The SQL WHERE clause is used to filter data in a database table based on specific conditions. It allows you to specify criteria that must be met for a record to be included in the result set.

2. Can the SQL WHERE clause be used with multiple conditions?

Yes, the SQL WHERE clause can be used with multiple conditions by using logical operators such as AND, OR, and NOT. This allows for more complex filtering of data in a database table.

3. How does the SQL WHERE clause handle repetitions in data?

The SQL WHERE clause does not handle repetitions in data. It simply filters the data based on the specified conditions. If there are repetitions in the data, all of the matching records will be included in the result set.

4. What is the default order of records in a SQL WHERE clause?

The default order of records in a SQL WHERE clause is based on the order in which they were inserted into the table. This can be changed by using the ORDER BY clause, which allows you to specify a different sorting order.

5. Can the SQL WHERE clause be used to update or delete records?

No, the SQL WHERE clause is used for filtering data and cannot be used to update or delete records. To update or delete records, you would need to use the UPDATE or DELETE statement with a WHERE clause to specify which records should be affected.

Similar threads

  • Programming and Computer Science
Replies
7
Views
442
  • Programming and Computer Science
Replies
5
Views
2K
Replies
0
Views
490
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
5
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
5
Views
288
Back
Top