I have about 6 tables where some of the columns are identical. Do I have to know which tables contain the column I'm querying on or is there a way to write an SQL query such that I can reference a column and the database will scan the tables looking for a specific column?
For example, assume table1, table3, and table5 all contain the column 'Population'. Do I have to specify in my query that I want to retrieve information from 'Population' in tables table1, table2, and table3, or can is there a way to only specify that I want information from the 'Population' column without specifying any tables?
select table1.population as pop1, table2.population as pop2, table5.population as pop3
from table1, table2, table5;
This will return 3 columns showing the population from each table.
select population
from table1
union
select population
from table2
union
select population
from table5;
This will return a long list of populations in one column.
Related
How can I get final output based on table 1 and table 2 in Big Query
Table 1
Table 2
Final Output
You can use union all. If the columns are in the same order:
select *
from table1
union all
select *
from table2;
In general, though, it is better to list out the column names instead of using *. Note that in the result set, the names from the first select are used for the result set.
I have two tables with same column names. There are a total 40 columns in each table. Both the tables have same unique IDs. If I perform an inner join on the ID columns I get a match on 80% of the data. However, I would like to see if this match has exactly same data in each of the columns.
If there were a few rows like say 50-100 I could have performed a simple union operation ordered by ID and manually checked for the data. But both the tables contain more than 5000 records.
Is a join on each of the columns a valid solution for this or do I need to perform concatenation?
Suppose you have N columns, you can add GROUP BY COL1,COL2,....COLN
select * from table1
union all
select * from table2
group by COL1, COL2, ... , COLN
having count(*)>1;
Reference: link
I'm trying to optimize a SQL query that uses a GROUP BY across multiple tables. Essentially, I have multiple tables which all contain a PID column, and the output I need is a record of every PID in all of the tables as well as a count of how many records across all of those tables contain that PID. When trying to use GROUP BY PID, I get a "column ambiguously defined" error if using multiple tables. Here is an example of the code I am using to retrieve the proper data from one table (can ignore the where clause):
select pid, count(*)
from table1
where vendor_id in(1,2)
and delay_code <=23
and age between 18 and 49 and sex = 'M'
group by pid
Essentially, I want to do this across a group of tables (i.e. table1, table2, table3 etc), but can't figure out how to do so without getting a "column ambiguously defined" error.
You need to identify what record you are referencing. You can do that either by specifying the table, or using an alias. Aliases are required when you have multiple references to the same table.
Specify table:
SELECT table1.pid, COUNT(*)
FROM table1
GROUP BY table1.pid
Use alias:
SELECT t1.pid, COUNT(*)
FROM table1 AS t1
GROUP BY t1.pid
I've got a strange situation with two SQL queries that aren't producing the expected results. Here are the queries:
Query 1:
SELECT DISTINCT SomeCharValue
FROM Table1
JOIN Table2
ON Table1.SomeCharValue = Table2.SomeCharValue
ORDER BY SomeCharValue
Query 2:
SELECT DISTINCT SomeCharValue
FROM Table1
JOIN Table2
ON Table1.SomeCharValue <> Table2.SomeCharValue
ORDER BY SomeCharValue
I have two tables with columns of varchar(15). Table2 is essentially a small subset of the values in Table1, thus Table1 has all values stored in Table2. The problem is, the two queries should never produce the same results, yet they do. Both queries will produce the same result for certain values; for example, if Table1 and Table2 contain the word 'hello', then Query 1 should return it, while Query 2 should not. However, BOTH queries return 'hello'. It doesn't make sense that 'hello' in both tables is equal and not equal at the same time. I ran a length query to test the values, and some were a different size with trailing white spaces, but even after changing these to be an exact match, and verifying the hexadecimal value of the characters to be the same, the same results occur. I can't compare numeric key fields since there is no key relationship between these tables. I can only compare the exact character values in the columns. Any ideas?
Imagine you have table1 containing a and b as separate rows, and table2 has the exact same contents.
Now for your second query, table1's row a will be compared to both the rows in table2. It will pass the ON clause when comparing to row b in table2, and hence a will be in your result set. Similarly for the b row in table1 which will pass the ON clause when compared to the a row in table2.
You could rewrite the query as
SELECT DISTINCT SomeCharValue
FROM TABLE1
WHERE SomeCharValue NOT IN (SELECT DISTINCT SomeCharValue FROM Table2)
ORDER BY SomeCharValue
Did you try to use NOT LIKE instead of <>
I need to compare two tables in different databases and check whether the data in both tables are matching or not.
The compare should return a result showing rows that don't match using an exact column to column data check.
Is this possible in PL-SQL?
To return all rows in table1 that do not match exactly the rows in table2:
select * from table1 except select * from table2
And to return all rows in table1 that match exactly what is in table2:
select * from table1 intersect select * from table2