How to create a SELECT query FROM "TABLE1 AND TABLE2" - sql

I have a PostgreSQL database, with only SELECT permissions. In this DB there are two tables with the same structure (the same columns).
I need to write several query in each table and join the results.
There is a way for writing a query like this one?
SELECT
field1,
field2,
field3
FROM
table1
AND
table2
WHERE
condition;
Select from 2 tables. Query = table1 OR table1 + table2 have no answer and it is not my question.

UNION ALL
SELECT field1, field2, field3
FROM table1
WHERE condition
UNION ALL
SELECT field1, field2, field3
FROM table2
WHERE condition;
Or to simplify your WHERE condition
SELECT * FROM
( SELECT field1, field2, field3
FROM table1
UNION ALL
SELECT field1, field2, field3
FROM table2
)
WHERE condition;

You can use Union:
SELECT
field1,
field2,
field3
FROM
table1
UNION
SELECT
field1,
field2,
field3
FROM
table2

SELECT * FROM
( SELECT field1, field2, field3
FROM table1
UNION ALL
SELECT field1, field2, field3
FROM table2
)
WHERE condition;

Related

How to use LIKE statement with multiple values from another field?

I want to select all records where field contains values from another field. How do I do that? Here is a code that I am trying.
select field1 , field2, field3
from table1
where field1 like '%'+(select distinct field4 from table2)+'%'
Thanks in advance.
Just do your like as a join condition:
select field1 , field2, field3
from table1
join (select distinct field4 from table2) x
on field1 like '%'+field4+'%'
Using the original structure of your query, you can do:
select field1, field2, field3
from table1 t1
where exists (select 1
from table2
where t1.field1 like '%' + field4 + '%'
);
The advantage of this method is that it will not duplicate records in table1. For instance, if there are two rows in table2 with the values 'a' and 'b' respectively and one row in table1 with the value 'ab', then this method will only return the row from table1 once.

ORACLE Select and group by excluding one field

I have a very simple query (on Oracle 11g) to select 3 fields:
select field1, field2, field3, count(*) from table
where...
group by field1, field2, field3
having count(*) > 10;
Now, what I need, is exclude "field3" from the "group by" since I only need field 1 and 2 to be grouped, but I also need field3 in the output.
As far I know, all the fields in the select must be reported also in "group by", so how can I handle that?
Thanks
Lucas
select t.field1, t.field2, t.field3, tc.Count
from table t
inner join (
select field1, field2, count(*) as Count
from table
where...
group by field1, field2
having count(*) > 10
) tc on t.field1 = tc.field1 and t.field2 = tc.field2
Use the analytical version of the "count" function:
select * from (
select field1, field2, field3, count(*) over(partition by field1, field2) mycounter
from table )
--simulate the having clause
where mycounter > 10;
If you don't group by field3 anymore, there can suddenly be different field3 per group. You must decide which one to show, e.g. the maximum:
select field1, field2, max(field3), count(*) from table
where...
group by field1, field2
having count(*) > 10;
The only way I know how to handle that is to first isolate the Field1 and Field2 data and create a new table, then link it back to the original table adding in Field3.
Select Table2.Field1, Table2.Field2, Table1.Field3
From
(Select Field1, max(Field2) as Field2
From Table1) Table2
Where Table2.Field1 = Table1.Field1
And Table2.Field2 = Table1.Field2
Group By
Table2.Field1, Table2.Field2, Table1.Field3

Refer to other SQL SELECT statements aliased as tables in FROM clause

I have a very large query that follows the format below:
select ...
from ( select field1,
field2
from some_table ) table1,
( select field1,
field3
from other_table ) table2
where .....
Is is possible for me to refer to one of the tables "defined" in the from clause, lets say table1, in one of the other table definitions in the from clause?
For example:
select ....
from ( select field1,
field2
from some_table ) table1,
( select table1.field1,
field3
from other_table,
table1 ) table2
where .....
Disclaimer: What I am trying to do is not as simple as the example above. The example is simply to illustrate the idea.
WITH
table1 AS
(
SELECT field1, field2
FROM some_table
),
table2 AS
(
SELECT field1, field2
FROM other_table, table1
)
SELECT *
FROM table2
If you are using SQL 2005, you can use Common Table Expressions for doing what you are trying; Quassnoi gives us an example, in Oracle I don't know how to achieve it though

Copy record to another table adding fields

I have 2 tables:
tab1 (field1, field2, field3)
tab2 (field1, field2,field3, field4)
I want to copy a record from tab1 to tab2 taking all the fields and adding a value for field4.
How can I select field1, field2 and field3 from tab2 and also add a value? I know that SELECT and VALUES in a INSERT query are mutually exclusive.
Thanks in advance.
Gustavo.
I don't know Oracle, but in Ms SQL it works like this:
insert into tab2 (field1, field2, field3, field4)
select field1, field2, field3, 'New Value' from tab1

Generate field in MySQL SELECT

If I've got a table containing Field1 and Field2 can I generate a new field in the select statement? For example, a normal query would be:
SELECT Field1, Field2 FROM Table
And I want to also create Field3 and have that returned in the resultset... something along the lines of this would be ideal:
SELECT Field1, Field2, Field3 = 'Value' FROM Table
Is this possible at all?
SELECT Field1, Field2, 'Value' Field3 FROM Table
or for clarity
SELECT Field1, Field2, 'Value' AS Field3 FROM Table
Yes - it's very possible, in fact you almost had it!
Try:
SELECT Field1, Field2, 'Value' AS `Field3` FROM Table