Why does using a subquery inside a left join give a completely different answer than an equivalent table? - sql

I'm using a left join in an Access query to get a table with a column added that only applies to a few rows. When I use a subquery as the table being joined it gives me a final table with the new column having the same value for all the rows. When I build a table that gives the same exact output as the subquery it works how I want it to. Why does a subquery and a table give me different results if they look the same.
I've looked over other questions about the difference between a table and a derived table(what I think I get using the subquery) and there doesn't seem to be a difference between the two.
SELECT *
FROM Table1
LEFT JOIN
(SELECT *, "P" AS PColumn FROM TableX, TableY WHERE TableX.x = TableY.y) AS Table2
ON (Table1.x = Table2.x)
WHERE Table1.X > 2
Every value in PColumn is "P". When I build a table that looks exactly like the subquery result and use that in place of the subquery, only row 5 has a PColumn value of "P" and the rest are null, which is what I want. The subquery and the table have the exact same values but different outputs in the outer SELECT statement

As inferred by the comments, this looks to be another occurrence of a bug present in the query optimiser of the JET database engine used by MS Access when evaluating outer joins, as described by Allen Browne in his article here.
Consider the following MCVE demonstrating the bug:
Table1
+----+
| ID |
+----+
| 1 |
| 2 |
+----+
Table2
+----+
| ID |
+----+
| 1 |
+----+
SQL
select * from table1 left join (select table2.id, "x" as X from table2) q on table1.id = q.id
Should return:
+-----------+------+---+
| table1.id | q.id | X |
+-----------+------+---+
| 1 | 1 | x |
| 2 | | |
+-----------+------+---+
Actually returns:
+-----------+------+---+
| table1.id | q.id | X |
+-----------+------+---+
| 1 | 1 | x |
| 2 | | x |
+-----------+------+---+

After confirming that my previous suggestions do not resolve the bug, instead leverage the bug to obtain expected results:
SELECT *
FROM Table1
LEFT JOIN
(SELECT TableX.x, Iif(TableX.x Is Null, Null, "P") AS PColumn
FROM TableX INNER JOIN TableY
ON TableX.x = TableY.y) AS Table2
ON (Table1.x = Table2.x)
WHERE Table1.X > 2
If Allen Brown is correct, the Access query optimizer executes a calculated field (i.e. static value) AFTER the join, so just make the field consider the existence of a normally non-null to reproduce correct outer join (i.e. left join) results.

Related

Join tables with unknown number of rows without repeating column that is joined by

Here's my quandary:
I need to join all columns of two tables based on a primary key, but I don't want to repeat the primary key in the results.
The second table has the primary key and then unknown number and names of columns.
So essentially I want
SELECT * (except for b.PK) FROM
TableA a
JOIN TableB b ON a.PK = b.PK
The obvious solution would be to select all columns explicitly from table a except for a.PK, but let's say that I don't know the number or names of columns in table a either (except I know it has the PK).
So to sum:
How do I join two tables by their PKs, where I don't know the rest of their columns explicitly, and without repeating the PK in the results?
EDIT: (Using T-SQL with SQL Server)
Something like SELECT * except column foo FROM ... doesn't exist. But you can use a natural join, which eliminates redundant columns. You haven't mentioned your RDBMS, so here's an explanation from the MySQL manual. A natural join is standard SQL though.
The columns of a NATURAL join or a USING join may be different from
previously. Specifically, redundant output columns no longer appear,
and the order of columns for SELECT * expansion may be different from
before.
Consider this set of statements:
CREATE TABLE t1 (i INT, j INT);
CREATE TABLE t2 (k INT, j INT);
INSERT INTO t1 VALUES(1,1);
INSERT INTO t2 VALUES(1,1);
SELECT * FROM t1 NATURAL JOIN t2;
SELECT * FROM t1 JOIN t2 USING (j);
Previously, the statements produced this output:
+------+------+------+------+
| i | j | k | j |
+------+------+------+------+
| 1 | 1 | 1 | 1 |
+------+------+------+------+
+------+------+------+------+
| i | j | k | j |
+------+------+------+------+
| 1 | 1 | 1 | 1 |
+------+------+------+------+
In the first SELECT statement, column j appears in both tables and
thus becomes a join column, so, according to standard SQL, it should
appear only once in the output, not twice. Similarly, in the second
SELECT statement, column j is named in the USING clause and should
appear only once in the output, not twice. But in both cases, the
redundant column is not eliminated. Also, the order of the columns is
not correct according to standard SQL.
Now the statements produce this output:
+------+------+------+
| j | i | k |
+------+------+------+
| 1 | 1 | 1 |
+------+------+------+
+------+------+------+
| j | i | k |
+------+------+------+
| 1 | 1 | 1 |
+------+------+------+
The redundant column is eliminated and the column order is correct
according to standard SQL

Difference between natural join and inner join

What is the difference between a natural join and an inner join?
One significant difference between INNER JOIN and NATURAL JOIN is the number of columns returned.
Consider:
TableA TableB
+------------+----------+ +--------------------+
|Column1 | Column2 | |Column1 | Column3 |
+-----------------------+ +--------------------+
| 1 | 2 | | 1 | 3 |
+------------+----------+ +---------+----------+
The INNER JOIN of TableA and TableB on Column1 will return
SELECT * FROM TableA AS a INNER JOIN TableB AS b USING (Column1);
SELECT * FROM TableA AS a INNER JOIN TableB AS b ON a.Column1 = b.Column1;
+------------+-----------+---------------------+
| a.Column1 | a.Column2 | b.Column1| b.Column3|
+------------------------+---------------------+
| 1 | 2 | 1 | 3 |
+------------+-----------+----------+----------+
The NATURAL JOIN of TableA and TableB on Column1 will return:
SELECT * FROM TableA NATURAL JOIN TableB
+------------+----------+----------+
|Column1 | Column2 | Column3 |
+-----------------------+----------+
| 1 | 2 | 3 |
+------------+----------+----------+
The repeated column is avoided.
(AFAICT from the standard grammar, you can't specify the joining columns in a natural join; the join is strictly name-based. See also Wikipedia.)
(There's a cheat in the inner join output; the a. and b. parts would not be in the column names; you'd just have column1, column2, column1, column3 as the headings.)
An inner join is one where the matching row in the joined table is required for a row from the first table to be returned
An outer join is one where the matching row in the joined table is not required for a row from the first table to be returned
A natural join is a join (you can have either natural left or natural right) that assumes the join criteria to be where same-named columns in both table match
I would avoid using natural joins like the plague, because natural joins are:
not standard sql [SQL 92] and therefore not portable, not particularly readable (by most SQL coders) and possibly not supported by various tools/libraries
not informative; you can't tell what columns are being joined on without referring to the schema
your join conditions are invisibly vulnerable to schema changes - if there are multiple natural join columns and one such column is removed from a table, the query will still execute, but probably not correctly and this change in behaviour will be silent
hardly worth the effort; you're only saving about 10 seconds of typing
A natural join is just a shortcut to avoid typing, with a presumption that the join is simple and matches fields of the same name.
SELECT
*
FROM
table1
NATURAL JOIN
table2
-- implicitly uses `room_number` to join
Is the same as...
SELECT
*
FROM
table1
INNER JOIN
table2
ON table1.room_number = table2.room_number
What you can't do with the shortcut format, however, is more complex joins...
SELECT
*
FROM
table1
INNER JOIN
table2
ON (table1.room_number = table2.room_number)
OR (table1.room_number IS NULL AND table2.room_number IS NULL)
SQL is not faithful to the relational model in many ways. The result of a SQL query is not a relation because it may have columns with duplicate names, 'anonymous' (unnamed) columns, duplicate rows, nulls, etc. SQL doesn't treat tables as relations because it relies on column ordering etc.
The idea behind NATURAL JOIN in SQL is to make it easier to be more faithful to the relational model. The result of the NATURAL JOIN of two tables will have columns de-duplicated by name, hence no anonymous columns. Similarly, UNION CORRESPONDING and EXCEPT CORRESPONDING are provided to address SQL's dependence on column ordering in the legacy UNION syntax.
However, as with all programming techniques it requires discipline to be useful. One requirement for a successful NATURAL JOIN is consistently named columns, because joins are implied on columns with the same names (it is a shame that the syntax for renaming columns in SQL is verbose but the side effect is to encourage discipline when naming columns in base tables and VIEWs :)
Note a SQL NATURAL JOIN is an equi-join**, however this is no bar to usefulness. Consider that if NATURAL JOIN was the only join type supported in SQL it would still be relationally complete.
While it is indeed true that any NATURAL JOIN may be written using INNER JOIN and projection (SELECT), it is also true that any INNER JOIN may be written using product (CROSS JOIN) and restriction (WHERE); further note that a NATURAL JOIN between tables with no column names in common will give the same result as CROSS JOIN. So if you are only interested in results that are relations (and why ever not?!) then NATURAL JOIN is the only join type you need. Sure, it is true that from a language design perspective shorthands such as INNER JOIN and CROSS JOIN have their value, but also consider that almost any SQL query can be written in 10 syntactically different, but semantically equivalent, ways and this is what makes SQL optimizers so very hard to develop.
Here are some example queries (using the usual parts and suppliers database) that are semantically equivalent:
SELECT *
FROM S NATURAL JOIN SP;
-- Must disambiguate and 'project away' duplicate SNO attribute
SELECT S.SNO, SNAME, STATUS, CITY, PNO, QTY
FROM S INNER JOIN SP
USING (SNO);
-- Alternative projection
SELECT S.*, PNO, QTY
FROM S INNER JOIN SP
ON S.SNO = SP.SNO;
-- Same columns, different order == equivalent?!
SELECT SP.*, S.SNAME, S.STATUS, S.CITY
FROM S INNER JOIN SP
ON S.SNO = SP.SNO;
-- 'Old school'
SELECT S.*, PNO, QTY
FROM S, SP
WHERE S.SNO = SP.SNO;
** Relational natural join is not an equijoin, it is a projection of one. – philipxy
A NATURAL join is just short syntax for a specific INNER join -- or "equi-join" -- and, once the syntax is unwrapped, both represent the same Relational Algebra operation. It's not a "different kind" of join, as with the case of OUTER (LEFT/RIGHT) or CROSS joins.
See the equi-join section on Wikipedia:
A natural join offers a further specialization of equi-joins. The join predicate arises implicitly by comparing all columns in both tables that have the same column-names in the joined tables. The resulting joined table contains only one column for each pair of equally-named columns.
Most experts agree that NATURAL JOINs are dangerous and therefore strongly discourage their use. The danger comes from inadvertently adding a new column, named the same as another column ...
That is, all NATURAL joins may be written as INNER joins (but the converse is not true). To do so, just create the predicate explicitly -- e.g. USING or ON -- and, as Jonathan Leffler pointed out, select the desired result-set columns to avoid "duplicates" if desired.
Happy coding.
(The NATURAL keyword can also be applied to LEFT and RIGHT joins, and the same applies. A NATURAL LEFT/RIGHT join is just a short syntax for a specific LEFT/RIGHT join.)
Natural Join: It is combination or combined result of all the columns in the two tables.
It will return all rows of the first table with respect to the second table.
Inner Join: This join will work unless if any of the column name shall be sxame in two tables
A Natural Join is where 2 tables are joined on the basis of all common columns.
common column : is a column which has same name in both tables + has compatible datatypes in both the tables.
You can use only = operator
A Inner Join is where 2 tables are joined on the basis of common columns mentioned in the ON clause.
common column : is a column which has compatible datatypes in both the tables but need not have the same name.
You can use only any comparision operator like =, <=, >=, <, >, <>
Natural Join : A SQL Join clause combines fields from 2 or more tables in a relational database. A natural join is based on all columns in two tables that have the same name and selected rows from the two tables that have equal values in all matched columns.
--- The names and data types of both columns must be the same.
Using Clause : In a natural join,if the tables have columns with the same names but different data types, the join causes and error.To avoid this situation, the join clause can be modified with a USING clause. The USING clause specifies the columns that should be used for the join.
difference is that int the inner(equi/default)join and natural join that in the natuarl join common column win will be display in single time but inner/equi/default/simple join the common column will be display double time.
Inner join and natural join are almost same but there is a slight difference between them. The difference is in natural join no need to specify condition but in inner join condition is obligatory. If we do specify the condition in inner join , it resultant tables is like a cartesian product.
mysql> SELECT * FROM tb1 ;
+----+------+
| id | num |
+----+------+
| 6 | 60 |
| 7 | 70 |
| 8 | 80 |
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+----+------+
6 rows in set (0.00 sec)
mysql> SELECT * FROM tb2 ;
+----+------+
| id | num |
+----+------+
| 4 | 40 |
| 5 | 50 |
| 9 | 90 |
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+----+------+
6 rows in set (0.00 sec)
INNER JOIN :
mysql> SELECT * FROM tb1 JOIN tb2 ;
+----+------+----+------+
| id | num | id | num |
+----+------+----+------+
| 6 | 60 | 4 | 40 |
| 7 | 70 | 4 | 40 |
| 8 | 80 | 4 | 40 |
| 1 | 1 | 4 | 40 |
| 2 | 2 | 4 | 40 |
| 3 | 3 | 4 | 40 |
| 6 | 60 | 5 | 50 |
| 7 | 70 | 5 | 50 |
| 8 | 80 | 5 | 50 |
.......more......
return 36 rows in set (0.01 sec)
AND NATURAL JOIN :
mysql> SELECT * FROM tb1 NATURAL JOIN tb2 ;
+----+------+
| id | num |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+----+------+
3 rows in set (0.01 sec)
Inner join, join two table where column name is same.
Natural join, join two table where column name and data types are same.

What is the syntax to return a user defined string in a left outer join?

I have hit a mental blind spot: - in spite of RTFM and other material I can't see this.
If I have a LEFT OUTER JOIN, what syntax is used to return a column for the right table if it exists, or a user defined string if it does not exist?
user defined string = "My string"
Table 1
id | text
-----------
1 | bloop
2 | grrrr
Table 2
id | flange
-----------
2 | whiz
Desired result
id | text | flange
-------------------
1 | bloop | My string
2 | grrrr | whiz
The COALESCE function in Firebird 1.5 and higher can convert NULL to most anything else. This enables you to perform an on-the-fly conversion and use the result in your further processing, without the need for “if (MyExpression is null) then” or similar constructions.
see Converting to and from NULL
e.g.
SELECT id,text,COALESCE(flange,'MY string') as flange from ...
SELECT Table1.id,
ISNULL(Table2.flange, 'UserDefinedString') as flange
FROM
Table1
LEFT JOIN Table2
ON Table1.id = Table2.id
SELECT Table1.id,
COALESCE(Table2.flange, 'UserDefinedString') as flange
FROM
Table1
LEFT JOIN Table2
ON Table1.id = Table2.id

My SQL query within a query

I have 2 tables that I am trying to combine in a specific way
Table 1: ‘part_defs’ Table 2 Items_part_values
in value_defs:
ID | Name
-------------
1 | color
2 | size
3 | weight
in Items_part_values
ItemID | valueID | Value
-------------------------
10 | 1 | red
11 | 1 | blue
What I need is a query where for a given item all the rows from value_defs appear and if they have a value in Items_part_values the value.
So for Item 11 I want
ID | Name | Value
--------------------
1 | color | red
2 | size | NULL
3 | weight | NULL
I’m new to MySQL, in access I would have created a subquery with the ItemID as a parameter and then done a Left Join with value_defs on the result.
Is there a way of doing something similar in MySQL?
Thanks
Use:
SELECT p.id,
p.name,
ipv.value
FROM PART_DEFS p
LEFT JOIN ITEMS_PART_VALUES ipv ON ipv.valueid = p.id
AND ipv.itemid = ?
Replace the "?" with the itemid you want to search for.
This means all the PARTS_DEF rows will be returned, and if the ITEMS_PART_VALUES.valueid matches the PART_DEFS.id value, then the ITEMS_PART_VALUES.value value will be displayed for the item you are looking for. If there's no match, the value column will be NULL for that record.
There's a difference in OUTER JOINs, when specifying criteria in the JOIN vs the WHERE clause. In the JOIN, the criteria is applied before the JOIN occurs while in the WHERE clause the criteria is applied after the JOIN.
Use a left join:
SELECT * FROM Table1 LEFT JOIN Table2 USING (ID);
Edit:
SELECT * FROM part_defs LEFT JOIN Items_part_values ON part_defs.ID = Items_part_values.valueID;

SQL query Help with OUTER JOIN?

I have two tables like this.
Table1
Column | Type |
---------+------------------+
cod | text |
value99 | double precision |
Table2
Column | Type |
---------+------------------+
cod | text |
value06 | double precision |
and i'd like to join them so i'd have something like
Column | Type |
---------+------------------+
cod | text |
value99 | double precision |
value06 | double precision |
the problem is that not all the codes are present in both tables, so if a code is not present in one of the tables it's value should be null.. In the end i'd like something like this
cod | value99 | value06 |
---------+------------------+------------------+
1 | 10 | 20 |
2 | 13 | NULL |
3 | NULL | 15 |
I think that its not possible using LEFT or RIGHT JOIN.. or maybe it is... any ideas? Thx=)
EDITED:
I've tried the FULL OUTER JOIN but the result is something like
code value code value
1 10 1 4
2 15 NULL NULL
NULL NULL 3 36
ANSWER!!!:
i found the answer thx to #Tobiasopdenbrouw :
SELECT test1.code,test1.value,test2.value FROM public.test1 LEFT OUTER JOIN public.test2 ON test1.code=test2.code
UNION
SELECT test2.code,test1.value,test2.value FROM public.test1 RIGHT OUTER JOIN public.test2 ON test1.code=test2.code
I'm guessing a bit, because your question doesn't describe the required output in great detail, but what you probably need is a helper query that will create a table with all the codes for you (a UNION of 2 SELECT querys). This helper table can then be LEFT JOINED to your 2 source tables).
Edit: I thought of the (FULL) OUTER JOIN answer myself, but in reading between the lines, I don't think that's what the OP really needs. But I can be wrong, of course.
Use a FULL OUTER JOIN.
Using Full Outer Joins
To retain the
nonmatching information by including
nonmatching rows in the results of a
join, use a full outer join. SQL
Server provides the full outer join
operator, FULL OUTER JOIN, which
includes all rows from both tables,
regardless of whether or not the other
table has a matching value.
Consider a join of the Product table
and the SalesOrderDetail table on
their ProductID columns. The results
show only the Products that have sales
orders on them. The ISO FULL OUTER
JOIN operator indicates that all rows
from both tables are to be included in
the results, regardless of whether
there is matching data in the tables.
You can include a WHERE clause with a
full outer join to return only the
rows where there is no matching data
between the tables. The following
query returns only those products that
have no matching sales orders, as well
as those sales orders that are not
matched to a product (although all
sales orders, in this case, are
matched to a product).
select
coalesce(t1.cod, t2.cod)
,t1.value99
,t2.value06
from
table1 t1
full outer join table2 t2 on t1.cod= t2.cod