When writing correlated sub-queries in BigQuery using a WHERE clause, it is resulting in an Error. LEFT OUTER JOIN cannot be used without a condition that is an equality of fields from both sides of the join.
However it works in other SQL engines, I am not understanding the error or how to rewrite the queries.
Different queries of the same type causing the same error.
SELECT
out.*
FROM
`august-monolith-205810.Assignment.students` AS out
WHERE
2 >= (
SELECT
COUNT(*)
FROM
`august-monolith-205810.Assignment.students` AS t1
WHERE
out.age < t1.age)
reporting students who are older than at most two other students.
A correlated subquery is a subquery that contains a reference to a table that also appears in the outer query. For example: SELECT * FROM t1 WHERE column1 = ANY (SELECT column1 FROM t2 WHERE t2. column2 = t1.
In a query that performs outer joins of more than two pairs of tables, a single table can be the null-generated table for only one other table. For this reason, you cannot apply the (+) operator to columns of B in the join condition for A and B and the join condition for B and C.
A left outer join is a method of combining tables. The result includes unmatched rows from only the table that is specified before the LEFT OUTER JOIN clause. If you are joining two tables and want the result set to include unmatched rows from only one table, use a LEFT OUTER JOIN clause or a RIGHT OUTER JOIN clause.
LEFT JOIN , also called LEFT OUTER JOIN , returns all records from the left (first) table and the matched records from the right (second) table. If there is no match for a specific record, you'll get NULLs in the corresponding columns of the right table.
Below is for BigQuery Standard SQL
#standardSQL
SELECT * EXCEPT(cnt)
FROM (
SELECT *, COUNT(1) OVER(ORDER BY age RANGE BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) cnt
FROM `august-monolith-205810.Assignment.students`
)
WHERE cnt BETWEEN 1 AND 2
Another, less effective and thus not recommended but still an option is to use CROSS JOIN to mimic non-equality LEFT JOIN
#standardSQL
SELECT ANY_VALUE(out).*
FROM `august-monolith-205810.Assignment.students` AS out
CROSS JOIN `august-monolith-205810.Assignment.students` AS t1
WHERE out.age > t1.age
GROUP BY out.id
HAVING COUNT(t1.age) <= 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With