Skip to main content
Tweeted twitter.com/StackDBAs/status/814043175551664128
deleted 69 characters in body
Source Link
Evan Carroll
  • 64.6k
  • 49
  • 251
  • 496

How can I identifiedidentify correlated subqueries that can not be rewritten as JOINs without using DISTINCT?

I am fairly new to SQL query tuning. I have been trying to understand how to write equivalent queries. While going through Prof. J. Widom's Stanford online course, I noticed that there are some subqueries that cannot be written by joinsProf. J. Widom's Stanford online video lectures, she makes mention that some subqueries that cannot be written by JOINs without the use of DISTINCT. For example, see this:

Sub queries in where clause: Two examples at the following time positions in the video:

How can I identified correlated subqueries that can not be rewritten as JOINs without using DISTINCT?

I am fairly new to SQL query tuning. I have been trying to understand how to write equivalent queries. While going through Prof. J. Widom's Stanford online course, I noticed that there are some subqueries that cannot be written by joins. For example, see this:

Sub queries in where clause: Two examples at the following time positions in the video:

How can I identify correlated subqueries that can not be rewritten as JOINs without using DISTINCT?

I am fairly new to SQL query tuning. I have been trying to understand how to write equivalent queries. While going through Prof. J. Widom's Stanford online video lectures, she makes mention that some subqueries that cannot be written by JOINs without the use of DISTINCT. For example, see this:

edited title
Source Link
Evan Carroll
  • 64.6k
  • 49
  • 251
  • 496

Subqueries How can I identified correlated subqueries that docan not have equivalent joins: Any rules to find thembe rewritten as JOINs without using DISTINCT?

Schema and table creation (for PostgreSQL) are as follows,

Subqueries that do not have equivalent joins: Any rules to find them?

Schema and table creation are as follows,

How can I identified correlated subqueries that can not be rewritten as JOINs without using DISTINCT?

Schema and table creation (for PostgreSQL) are as follows,

added 2111 characters in body
Source Link
Evan Carroll
  • 64.6k
  • 49
  • 251
  • 496
  1. 4:45 / 20:13 - GPA example

    4:45 / 20:13 - GPA example

    SELECT GPA
    FROM Student
    WHERE sID in (select sID from Apply where major = 'CS');
    
  2. 6:39 / 20:13 - students applied to CS but not to EE

    6:39 / 20:13 - students applied to CS but not to EE

    SELECT sID, sName
    FROM Student
    WHERE sID IN (select sID from Apply where major = 'CS')
      AND sID NOT IN (select sID from Apply where major = 'EE');
    

Sample Data

Schema and table creation are as follows,

CREATE TEMP TABLE college AS
SELECT cname::text, state::text, enrollment::int
FROM ( VALUES
  ('Stanford', 'CA', 15000),
  ('Berkeley', 'CA', 36000),
  ('MIT', 'MA', 10000),
  ('Cornell', 'NY', 21000)
) AS College(cname, state, enrollment);

CREATE TEMP TABLE student AS
SELECT sid::int, sname::text, gpa::real, sizehs::int
FROM ( VALUES
  (123, 'Amy', 3.9, 1000),
  (234, 'Bob', 3.6, 1500),
  (345, 'Craig', 3.5, 500),
  (456, 'Doris', 3.9, 1000),
  (567, 'Edward', 2.9, 2000),
  (678, 'Fay', 3.8, 200),
  (789, 'Gary', 3.4, 800),
  (987, 'Helen', 3.7, 800),
  (876, 'Irene', 3.9, 400),
  (765, 'Jay', 2.9, 1500),
  (654, 'Amy', 3.9, 1000),
  (543, 'Craig', 3.4, 2000)
) AS Student(sid, sname, gpa, sizehs);

CREATE TEMP TABLE apply AS
SELECT sid::int, cname::text, major::text, decision::text
FROM ( VALUES
  (123, 'Stanford', 'CS', 'Y'),
  (123, 'Stanford', 'EE', 'N'),
  (123, 'Berkeley', 'CS', 'Y'),
  (123, 'Cornell', 'EE', 'Y'),
  (234, 'Berkeley', 'biology', 'N'),
  (345, 'MIT', 'bioengineering', 'Y'),
  (345, 'Cornell', 'bioengineering', 'N'),
  (345, 'Cornell', 'CS', 'Y'),
  (345, 'Cornell', 'EE', 'N'),
  (678, 'Stanford', 'history', 'Y'),
  (987, 'Stanford', 'CS', 'Y'),
  (987, 'Berkeley', 'CS', 'Y'),
  (876, 'Stanford', 'CS', 'N'),
  (876, 'MIT', 'biology', 'Y'),
  (876, 'MIT', 'marine biology', 'N'),
  (765, 'Stanford', 'history', 'Y'),
  (765, 'Cornell', 'history', 'N'),
  (765, 'Cornell', 'psychology', 'Y'),
  (543, 'MIT', 'CS', 'N')
) AS apply(sid, cname, major, decision);
  1. 4:45 / 20:13 - GPA example
  2. 6:39 / 20:13 - students applied to CS but not to EE
  1. 4:45 / 20:13 - GPA example

    SELECT GPA
    FROM Student
    WHERE sID in (select sID from Apply where major = 'CS');
    
  2. 6:39 / 20:13 - students applied to CS but not to EE

    SELECT sID, sName
    FROM Student
    WHERE sID IN (select sID from Apply where major = 'CS')
      AND sID NOT IN (select sID from Apply where major = 'EE');
    

Sample Data

Schema and table creation are as follows,

CREATE TEMP TABLE college AS
SELECT cname::text, state::text, enrollment::int
FROM ( VALUES
  ('Stanford', 'CA', 15000),
  ('Berkeley', 'CA', 36000),
  ('MIT', 'MA', 10000),
  ('Cornell', 'NY', 21000)
) AS College(cname, state, enrollment);

CREATE TEMP TABLE student AS
SELECT sid::int, sname::text, gpa::real, sizehs::int
FROM ( VALUES
  (123, 'Amy', 3.9, 1000),
  (234, 'Bob', 3.6, 1500),
  (345, 'Craig', 3.5, 500),
  (456, 'Doris', 3.9, 1000),
  (567, 'Edward', 2.9, 2000),
  (678, 'Fay', 3.8, 200),
  (789, 'Gary', 3.4, 800),
  (987, 'Helen', 3.7, 800),
  (876, 'Irene', 3.9, 400),
  (765, 'Jay', 2.9, 1500),
  (654, 'Amy', 3.9, 1000),
  (543, 'Craig', 3.4, 2000)
) AS Student(sid, sname, gpa, sizehs);

CREATE TEMP TABLE apply AS
SELECT sid::int, cname::text, major::text, decision::text
FROM ( VALUES
  (123, 'Stanford', 'CS', 'Y'),
  (123, 'Stanford', 'EE', 'N'),
  (123, 'Berkeley', 'CS', 'Y'),
  (123, 'Cornell', 'EE', 'Y'),
  (234, 'Berkeley', 'biology', 'N'),
  (345, 'MIT', 'bioengineering', 'Y'),
  (345, 'Cornell', 'bioengineering', 'N'),
  (345, 'Cornell', 'CS', 'Y'),
  (345, 'Cornell', 'EE', 'N'),
  (678, 'Stanford', 'history', 'Y'),
  (987, 'Stanford', 'CS', 'Y'),
  (987, 'Berkeley', 'CS', 'Y'),
  (876, 'Stanford', 'CS', 'N'),
  (876, 'MIT', 'biology', 'Y'),
  (876, 'MIT', 'marine biology', 'N'),
  (765, 'Stanford', 'history', 'Y'),
  (765, 'Cornell', 'history', 'N'),
  (765, 'Cornell', 'psychology', 'Y'),
  (543, 'MIT', 'CS', 'N')
) AS apply(sid, cname, major, decision);
Source Link
Loading