Enter multiple rows explicitly in single select statement - sql

I am testing a query and want to use SELECT to get some initial data like so:
SELECT 1,2,3
UNION
SELECT 2,3,4
Is there a syntax to fold these two selects into one or do I have to use a UNION statement for each row?

You can use values clause
select t.* from (values(1,2,3), (2,3,4)) as t(col1,col2,col3)

If you want to display the result as one result set then you have to use union/union all

Related

How to run two ORACLE SQL Queries together in TOAD?

I want to run two Oracle SQL queries together inside TOAD.
first query:
SELECT * FROM OT_SO_HEAD WHERE SOH_ANNOTATION = 'ECSO10012791'
and second:
SELECT * FROM OT_SO_ITEM WHERE SOI_SOH_SYS_ID = '30977853'
Please guide
You can use UNION or UNION ALL.
The UNION operator is used to combine the result-set of two or more SELECT statements.
Every SELECT statement within UNION must have the same number of columns
The columns must also have similar data types
The columns in every SELECT statement must also be in the same order
Syntax:
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL.
right click in script window and select EXECUTE -> Execute via Toad Script Runner and reuse Toad

How to efficiently perform union of two queries with and without group by

I have a query that performs a union between two select statements one that uses group by and another that doesn't. The problem is I'm selecting the same columns and using the same fucntions in both select statements. It feels Im duplicating the code and I wish to know if there's a better way to write this
I've tried to use the normal union function to two select statements, but both select statements use the same functions.
Is there a way to simplify the following query without duplication?
Example:
select
sum(col1), sum(col2)....
from table
union
select sum(col1), sum(col2)...
from table
group by class
I require a table which is obtained by combining the result of the above.
The second query may have multiple categories and first query yields only one aggregated row
The objective is to compare the income and other details of the total population with one or more of categories within the population
Thanks in advance :)
You can add the WITH ROLLUP clause to your GROUP BY and it will add an aggregate row to the end of your output i.e.
SELECT SUM(col1), SUM(col2)...
FROM table
GROUP BY class WITH ROLLUP
You have not provided the sample data to check, but one approach can be using CASE WHEN in GROUP BY.
Following UNION
SELECT Sum(col1)
FROM tablename
WHERE id <> 1
UNION
SELECT Sum(col1)
FROM tablename
WHERE id = 1
GROUP BY class
Can be written as following using CASE
SELECT Sum(col1)
FROM tablename
GROUP BY CASE
WHEN id = 1 THEN 0
ELSE 1
END

I need to combine two results from a SQL select query

I Have two Select queries that get results for two tables with the same column names.
SELECT
labels.langjrd,
labels.id,
labels.lcphrase
FROM
labels
WHERE
labels.langjrd LIKE 'FRE%'
;
and
SELECT
labels.langjrd,
labels.id,
labels.lcphrase
FROM
labels
WHERE
labels.langjrd LIKE 'ENG%'
;
When I run the query I want to put all the results into one table. I read about union query but when I tried it, it didn’t work. I don't want to overwrite all the duplicate data I just want to have the second select results be added to the bottom of the first select results.
With UNION ALL you can combine results from two queries like this:
SELECT
labels.langjrd,labels.id,labels.lcphrase
FROM
labels
WHERE
labels.langjrd LIKE 'FRE%'
UNION ALL
SELECT
labels.langjrd,labels.id,labels.lcphrase
FROM
labels
WHERE
labels.langjrd LIKE 'ENG%';
You read about UNION which does a similar thing, but it filters out duplicate results. So it's similar as UNION ALL, but not the same.
But since you are actually querying the exact same columns from the same table, you can just put the two conditions in the where clause and split them using or. That way you will get all records that match either of the conditions.
SELECT
labels.langjrd,labels.id,labels.lcphrase
FROM
labels
WHERE
labels.langjrd LIKE 'FRE%' OR
labels.langjrd LIKE 'ENG%';

Use distinct keyword with two tables

I want to take unique values using DISTINCT keyword. I have two more tables.
Table Names:
(T1)Cand_details
Locationofwork
(T2)requirement_details
Locationofposting
I want to select these two table values by using keyword distinct. Is this possible?
select LocationOfWork
from cand_details
union
select Locationofposting
from requirement_details;
A UNION operator serves to combine data from multiple SELECT statements.
In this case, without the ALL keyword (UNION ALL), the UNION operator includes a DISTINCT function which will give you the unique locations across both tables.
Something like this?
SELECT DISTINCT Locationofwork, Locationofposting FROM Cand_details, Locationofposting
You should relate both tables with common fields if they have.
Select Locationofwork as "LOC"
From Cand_details
UNION Select Locationofposting as "LOC"
FROM requirement_details
Yes this is easily possible. You can encapsulate as many feilds as you like in the distinct ( ) clause.

Is it possible for a subquery to return two values?

Is it possible for a subquery to return two values onto the outer query?
Such as:
SELECT 1,
(SELECT COUNT(*), MAX(*) FROM test_table WHERE test=123)
FROM another_table
Or is there a better way to do this?
If you use the subquery in the FROM clause rather than the field list, then you can treat the output as a table and refer to the separate columns.
You are just selecting numbers as results so couldn't you just do:
SELECT 1, COUNT(*), MAX(*) FROM test_table WHERE test=123
Not possible
mysql> select 1, (select 2, 3) from dual;
ERROR 1241 (21000): Operand should contain 1 column(s)
If you are dealing with two tables and you what the results in one line, you should preform a join.
Hmm, it depends on what exactly you want to do with the data, you can join two tables using JOIN syntax, and one of the tables can actually be a subquery. I think that is probably what you want.
I'm not even user what your current query will do..
Documentation:
http://dev.mysql.com/doc/refman/5.0/en/join.html