Can SQL INTERSECT keyword be used to find an x-way intersection?
SELECT col FROM tab 1
INTERSECT
SELECT col FROM tab 2
INTERSECT
SELECT col FROM tab 3
Or am I better off just using an x-way INNER JOIN?
Related
Two different queries, produce two different result set.
Query 1:
Select a.customer_name, b.salary, c.manager_name
from
table123 a
left join table456 b on a.id=b.id
left join table789 c on a.id=c.id
Query2:
Select d.prty_id, e.party_val, f.prty_nme
from
table111 d
Left join table222 e on d.id=e.id
Left join table333 f on d.id=f.id
Now I have to write one single query, which will merge this above two query's result set and display one single result set.
Can anyone please help me on this.
Output value I need to insert in a new table whose column will be like below:
customer_name salary manager_name prty_id party_val prty_nme
According to your expected result, you can use UNION or UNION ALL.
The Oracle UNION operator is used to combine the result sets of 2 or more Oracle SELECT statements. It removes duplicate rows between the various SELECT statements.
The Oracle UNION ALL operator is used to combine the result sets of 2 or more SELECT statements. It returns all rows from the query and it does not remove duplicate rows between the various SELECT statements.
Select con1,... , coln
from tab1
where...
union
Select con1,... , coln
from tab2
where...
Just make sure your data types match up.
I want to select the max() value of the modified_at column in each of several tables:
select
max(a.modified_at) as a_modified_at,
max(b.modified_at) as b_modified_at,
max(c.modified_at) as c_modified_at
from a, b, c;
This works correctly as long as each of the tables has at least 1 row.
The problem is that when just one of the tables has 0 rows, null is returned for all tables:
null,null,null
What is a solution that returns the correct values for the tables that do have rows?
PostgreSQL-10
Using OUTER JOINs should do it
select
max(a.modified_at) as a_modified_at,
max(b.modified_at) as b_modified_at,
max(c.modified_at) as c_modified_at
from a outer join b outer join c;
but a possibly simpler option would be to use 3 subqueries instead:
select
(select max(modified_at) from a) as a_modified_at,
(select max(modified_at) from b) as b_modified_at,
(select max(modified_at) from c) as c_modified_at;
I have five queries and each of them will return me single column multiple row output. I want to to write a function which will contain all of these queries.
Can anyone help?
query 1:
Select Col1 as X from Table1;
query 2:
Select Col3 as Y from Table2;
From a function I want to get a table which will have columns
X, Y
How to club these queries under single function?
Add a ROW_NUMBER() to each of the queries and join them by the row number.
Depending on number of rows returned by each of the query you'd join then by inner, left or full join.
Example below assumes that two queries return the same number of rows.
WITH
CTE1
AS
(
SELECT Col1 as X, ROW_NUMBER() OVER(ORDER BY Col1) AS rn
FROM Table1
)
,CTE2
AS
(
SELECT Col3 as Y, ROW_NUMBER() OVER(ORDER BY Col3) AS rn
FROM Table2
)
SELECT
CTE1.X, CTE2.Y
FROM
CTE1
INNER JOIN CTE2 ON CTE1.rn = CTE2.rn
Use the UNION operator:
SELECT
column_1
FROM
tbl_name_1
UNION ALL
SELECT
column_1
FROM
tbl_name_2;
If there is a relation between the two tables, try using a join.
Maybe a simple inner join would be possible here?
select Col1 as X from Table1
join
on Table1.Col1_name = Table2.col3_name
I'm trying to use the EXCEPT keyword in Oracle 10.1.0.2.0, but kept getting error 'Unknown Command'. I've tried googling around and someone said the keyword is MINUS, so I used MINUS, instead, but I still got the same error.
Any idea?
Thanks.
So here's my query.
I'm finding the name of students who enrolls in ALL courses with course number > 500
SELECT s.name
FROM Students s
WHERE NOT EXISTS
(
SELECT c.id
FROM Courses c
WHERE c.number > 500
MINUS
SELECT e.course_id
FROM Enrollment e
WHERE e.student_id = s.id
);
Oracle MINUS is an operator; it's equivalent to EXCEPT in SQL Server. Here is a previous post explaining the difference. Here's a trivial example:
SELECT a, b, c
FROM table_a
MINUS
SELECT a, b, c
FROM table_b
If you still have problems, add the complete query you are using to your question; it's likely a simple syntax error.
Oracle 20c will support EXCEPT/EXCEPT ALL keywords.
SELECT col1, col2
FROM t1
EXCEPT
SELECT col1, col2
FROM t2;
or EXCEPT ALL if you want to handle duplicates:
SELECT col1, col2
FROM t1
EXCEPT ALL
SELECT col1, col2
FROM t2;
4.6 Set Operators
Set operators combine the results of two component queries into a single result.
EXCEPT All distinct rows selected by the first query but not the second
EXCEPT ALL All rows selected by the first query but not the second including duplicates
I have two tables and want to compare rows on sqlite like this
table1 table2
field1 field1
a a
b d
c f
d g
e
f
g
h
i
and I want to produce result like this
result_table
field1
b
c
e
h
i
How is the syntax in sqlite?
Thanks
SELECT DISTINCT Field1
FROM Table1
WHERE Field1 Not IN
(SELECT DISTINCT Field1 FROM Table2)
SELECT columns1 FROM table1 EXCEPT SELECT columns2 FROM table2;
The SQLite EXCEPT clause returns all rows from the left SELECT statement that are not in the result of the second SELECT statement. The number of columns selected must be the same in both SELECT statements.
This works fine for small to medium size tables. Avoid for tables with millions of lines.
See Compound Select Statements and the documentation of the SQLite SELECT statement.