Table name after parenthesis notation - sql

I was going through some old-ish SQL code someone else had written that I couldn't quite understand. I have simplified its structure here, but if anyone can walk me through what exactly is going on, that would be appreciated! You may ignore the specific column operations as they were just examples.
SELECT table.*,
column1 - column2
AS 'col1 - col2',
...
columnn
AS 'coln'
FROM
(SELECT
...
) table
What I don't understand is the final line. I am assuming it is the definition of "table" in the FROM (SELECT ...) part, and the ) table part indicates the name of the defined table.
Thanks in advance!

An inner select needs an alias name
select alias_name.* from
(
select * from some_table ...
) alias_name

The 'table' in that final line is an alias for the subquery.
In T-SQL it is AFAIK mandatory to specify an alias for a subquery if you're selecting from a subquery.
You can name the alias whathever you want. It is perfectly ok to use it like this
select * from
( select * from ... ) as X
(The as keyword is not mandatory, but I always specify the alias-name using 'as').

Related

what is the meaning if in where clause we have an other value

WHERE ("z" = 'abc')) "test"
You need to show the entire query, but "test" is not part of the where clause. It looks like a table alias or a column alias. For example:
select "test".*
from (select t.*
from t
where ("z" = 'abc')
) "test"
I do not advise escaping column aliases or table aliases, but your code seems to be doing that.

SQL SELECT statement with a field's value from a value list

I need to query a SQLite database for some entries containing a field the value of which can be one of a defined list: 'Token1', 'Token2', ..., 'TokenN' - potentially a long one.
The straightforward SELECT statement would be something like
SELECT * FROM `my_table` WHERE `token` = 'Token1' OR `token` = 'Token2' OR ...
- far from elegant. I wonder, is there a better way to formulate the statement?
You can use IN Clause
SELECT * FROM `my_table`
WHERE `token` in ('Token1','Token2', 'Token3', ....);
Use the wildcard character %%.
SELECT * FROM `my_table` WHERE `token` like '%Token%'
If your defined list is or can be placed in a table, you can do the following:
SELECT
*
FROM [employeeName] Where dept In (Select dept From #tbl)

Using CONTAINS to find items IN a table

I'm trying to write a SP that will allow users to search on multiple name strings, but supports LIKE functionality. For example, the user's input might be a string 'Scorsese, Kaurismaki, Tarkovsky'. I use a split function to turn that string into a table var, with one column, as follows:
part
------
Scorsese
Kaurismaki
Tarkovsky
Then, normally I would return any values from my table matching any of these values in my table var, with an IN statement:
select * from myTable where lastName IN (select * from #myTableVar)
However, this only returns exact matches, and I need to return partial matches. I'm looking for something like this, but that would actually compile:
select * from myTable where CONTAINS(lastName, select * from #myTableVar)
I've found other questions where it's made clear that you can't combine LIKE and IN, and it's recommended to use CONTAINS. My specific question is, is it possible to combine CONTAINS with a table list of values, as above? If so, what would that syntax look like? If not, any other workarounds to achieve my goal?
I'm using SQL Server 2016, if it makes any difference.
You can use EXISTS
SELECT * FROM myTable M
WHERE
EXISTS( SELECT * FROM #myTableVar V WHERE M.lastName like '%'+ V.part +'%' )
Can your parser built the entire statement? Will that get you what you want?
select *
from myTable
where CONTAINS
(lastName,
'"Scorsese" OR "Kaurismaki" OR "Tarkovsky"'
)
This can be done using CHARINDEX function combined with EXISTS:
select *
from myTable mt
where exists(select 1 from #myTableVar
where charindex(mt.lastName, part) > 0
or charindex(part, mt.lastName) > 0)
You might want to omit one of the conditions in the inner query, but I think this is what you want.

What is the "DATA" keyword in T-SQL for?

I'm working with a database in T-SQL/SQL Server 2016 at the moment which has some stored procedures containing a keyword I'm not familiar with, namely the "DATA" suffix after a query:
SELECT * FROM dbo.TableName DATA
I'm struggling to find any documentation on what the purpose of this "DATA" keyword is. Could someone shed some light please?
It is not some specific keyword. It is just a table alias. Note that if you changed your select to
SELECT DATA.* FROM dbo.TableName DATA
it will work, as the table now has the "DATA" alias. For the same reason, this:
SELECT dbo.TableName.* FROM dbo.TableName DATA
will throw an error.
This is an alias for the table name, usually it is used if we are inner joining the same table more than one time, or when we need to call the table with a shortcut name.
For example if the table has a key named ID, then:
SELECT DATA.* FROM dbo.TableName DATA
where DATA.ID = "1"
is like
SELECT dbo.TableName.* FROM dbo.TableName
where TableName .ID = "1"

PL/SQL error with transposing string to rows

I am trying to spilt a string in my table into separate rows and then looking up those values in another table to see which values doesn't exists. This is the code I am using:
SELECT NAME, desc, LABEL, trim(x.column_value.extract('e/text()')) AS ID
from table1 T1, table (xmlsequence(xmltype('<e><e>' || replace(str_work,' ','</e><e>')||
'</e></e>').extract('e/e'))) x
where
NOT EXISTS (SELECT *
FROM
tabl2 T2
WHERE
T1.ID = x.ID) AND t1.str_work IS NOT NULL);
The code is giving an error:
ORA-00904: "X"."ID": invalid identifier
I am not able to figure out what the issue is. I would appreciate all your help and suggestions.
Thank you.
From the documentation (emphasis added):
You can use a column alias, c_alias, to label the immediately
preceding expression in the select list so that the column is
displayed with a new heading. The alias effectively renames the select
list item for the duration of the query. The alias can be used in the
ORDER BY clause, but not other clauses in the query.
So you can't refer to the ID alias in the subquery. That alias isn't coming from the inline view you've labelled as x anyway, though. You could solve both issues, I think, by repeating the trim in the subquery:
NOT EXISTS (SELECT *
FROM
tabl2 T2
WHERE
T1.ID = trim(x.column_value.extract('e/text()')))
AND t1.str_work IS NOT NULL);
But is that supposed to be referring to T2.ID - otherwise there is no correlation?
Alternatively you could introduce another inline view to avoid the repetition, but it's probably adding complexity for not much gain here.