The purpose of 'value' keyword in Oracle Select Statement - sql

Whats is the difference between:
select 'mari makan' value from dual ;
and
select 'mari makan' from dual ;
when querying from oracle table.
Precisely, what is the purpose of value keyword in above statement?

The first names the column value. It is more typically written using as:
select 'mari makan' as value
from dual ;
The second does not give a user-defined name to the column.

The difference between them is that the first query aliasing this column, and giving it a unique name, while the other one doesn't.
What does that mean?
SELECT * FROM (
SELECT 'asda' FROM dual
UNION
SELECT 'asdasda' FROM dual
)
WHERE ??? = ??
This query is not aliasing the column, if you would want to use this column in the other query, you will have a problem. I think the default is to name the column after what you selected, which means it will be named 'asda' .
As opposed to:
SELECT * FROM (
SELECT 'asda' Col1 FROM dual
UNION
SELECT 'asdasda' as Col1 FROM dual
) WHERE Col1 <> 'asda'
Which will let you call this column from an outer query.
The standard is to write it as SELECT <Col> AS <New_Name> but it can be written with out the AS as well.

Related

Oracle How to populate data when there is no query record

I want to execute the following SQL in PL/SQL
select did, n1, n2 ,n3
from t where t.did in (‘A’,’C’,’G’)
the result will be “no recode”
I wish the result can be like this
how should I write the sql
Ideally you should have some other table which maintains all possible did values. In absence of that, we can try using a CTE here:
WITH cte AS (
SELECT 'A' AS did FROM dual UNION ALL
SELECT 'B' FROM dual UNION ALL
SELECT 'C' FROM dual
)
SELECT
t1.did,
t2.n1,
t2.n2,
t3.n3
FROM cte t1
LEFT JOIN t t2
ON t1.did = t2.did;
Of course, this also means that should your table have more than one record matching a giving did value, that your result set would also have more than one record for that did value. If you always expect a single record per did value, then you would have to give us the logic behind that.

Sql Only String search

with temp(name) as (select 'abc' from dual
union select '123abc' from dual
union select '1abc3' from dual)
select * from temp where temp.name like '%[a-z]%'
why i am not able to get all the 3 records in output
, I am using Oracle SQL Developer 11g
Reason why i am using this query is : I want to check if Desired column Contains only String no Number and Special Character
You are mixing SQL Server and Oracle syntax:
SQL SERVER Demo:
with temp(name) as (
select 'abc' union all select '123abc' union select '1abc3'
)
select * from temp where temp.name like '%[a-z]%';
-- [] is T-SQL specific
ORACLE Demo:
with temp(name) as (select 'abc' from dual
union select '123abc' from dual
union select '1abc3' from dual)
select * from temp where regexp_like(temp.name, '[a-z]')
First of all, you are mixing concepts. You are trying to do a regular expression compare by using a normal LIKE operator. Secondly, you should tag your question with the database you are using. I am assuming that you are using oracle, given the dual table.
WITH temp(name) AS (SELECT 'abc' FROM dual
UNION SELECT '123abc' FROM dual
UNION SELECT '1abc3' FROM dual)
SELECT * FROM temp
WHERE REGEXP_LIKE(temp.name,'[a-z]+')
Also note that using the % wildcard character will not work as you expect it to in a regular expression match.
Fist, it is better to use union all rather than union, because union incurs overhead for removing duplicates.
Second, your like pattern is non-standard, supported only in SQL Server and Sybase. In other words, none of your names contain square braces or hyphens, so none match the pattern.
Does this do what you expect?
with temp(name) as (
select 'abc' from dual union all
select '123abc' from dual union all
select '1abc3' from dual
)
select *
from temp
where temp.name like '%a%';
This is only checking for syntax. It is not equivalent to your like pattern.
I speculate from the syntax that you are using Oracle. If so, then you can use regular expressions:
where regexp_like(temp.name, '[a-z]')
Most (but not all) databases support regular expressions, so you can express similar logic in most databases.

Oracle Query fetching table name alongwith column name

select trx_id,refernce number from
(select * from abcd_1_txt union
select * from abcd_2_txt union
select * from abcd_3_txt union
select * from abcd_4_txt)
where trx_id in (123,321,1234)
In the query all the tables are of same format, same column names and same number of columns.
After running this query, surely i will get some data.
My question --- is there any way to know from which of these tables, i am getting the output.
Try to add a column with number of query as below
select qrynum, trx_id,refernce number from
(select 1 as qrynum,* from abcd_1_txt union
select 2,* from abcd_2_txt union
select 3,* from abcd_3_txt union
select 4,* from abcd_4_txt)
where trx_id in (123,321,1234)
as Joe W said in the comment below you can also use name of the table instead of query number, short example:
select tabname, trx_id,refernce number from
(select 'abcd_1_txt' as tabname,* from abcd_1_txt union
...
where trx_id in (123,321,1234)
but both ways don't eliminate duplicates, so you can use union all instead of union. Other way to do that is to run quires separately with the condition
select * from abcd_1_txt where trx_id in (123,321,1234)
select * from abcd_2_txt where trx_id in (123,321,1234)
.
.
.

input string to table

I am doing some debugging in SQL for oracle 10g. I have a big input string which is used in "IN Clause" i.e.
select * from table where col in ('str2','str3','str4','str5',...)
i want to convert the in clause to rows or table?
Is there a way to do this i.e.
select 'str2','str3','str4','str5', .. from dual
but this outputs multiple columns and i want multiple rows?
Edit:
Here is what i am trying to do. suppose i have an excel data in tmp_table1 (cant create in reality) and tmp_table1 is same as the IN clause, then the below statement will give the missing keys.
SELECT *
FROM tmp_table1
WHERE unique_id NOT IN (
SELECT unique_id
FROM table1
WHERE unique_id IN
('str1', 'str2', 'str3', 'str4'))
now #andriy-m solution works if the in string is less than 4000. but what if its greater?
You are probably looking for this solution.
You can UNION the values into multiple rows:
SELECT 'str2' AS col FROM dual
UNION
SELECT 'str3' FROM dual
UNION
SELECT 'str4' FROM dual
UNION
SELECT 'str5' FROM dual

Column names for a table formed by a UNION

Given a couple of simple tables like so:
create table R(foo text);
create table S(bar text);
If I were to union them together in a query, what do I call the column?
select T.????
from (
select foo
from R
union
select bar
from S) as T;
Now, in mysql, I can apparently refer to the column of T as 'foo' -- the name of the matching column for the first relation in the union. In sqlite3, however, that doesn't seem to work. Is there a way to do it that's standard across all SQL implementations?
If not, how about just for sqlite3?
Correction: sqlite3 does allow you to refer to T's column as 'foo' after all! Oops!
Try to give an alias to columns;
select T.Col1
from (
select foo as Col1
from R
union
select bar as Col1
from S) as T;
or If the name of column is not necessary then T.* will be enough.
Although there is no spelled rule, we can use the column names from the first subquery in the union query to fetch the union results.
you only need column aliases only in first select (tested in SQl Server 2008 R2)
select T.Col1
from (
select 'val1' as Col1
union
select 'val2'
union
select 'val3'
) as T;