Home > Blockchain >  How to query two SQL tables and filter that data
How to query two SQL tables and filter that data

Time:03-17

I'm trying to gather data from two tables like this

SELECT table1.NAME, table2.NAME
FROM table1, table2
WHERE table1.NAME = table2.NAME

then i want to be able to query that output data with a new SQL statement is there a way of doing that?

CodePudding user response:

Sure, you can use a CTE:

with cte as
(SELECT table1.NAME, table2.NAME
FROM table1, table2
WHERE table1.NAME = table2.NAME)
select * from cte --here goes your statement

Please note that you should be using the modern explicit join syntax instead of the old one you're using:

with cte as
(SELECT table1.NAME, table2.NAME
FROM table1 inner join table2
on table1.NAME = table2.NAME)
select * from cte --here goes your statement
  • Related