Subselect in pgSQL - sql

I'm trying to do a subselect in pgsql aka postgresql and the example I found doesn't work:
SELECT id FROM (SELECT * FROM table);

I just needed to add an AS for the subselect, like so:
SELECT id FROM (SELECT * FROM table) AS aliasname;

I think you need something like:
SELECT * FROM table WHERE id IN (SELECT id FROM table2);
I don't understand what your non-working subquery is attempting to do, it seems like you could just say SELECT id FROM table because presently its not valid SQL92 syntax.

Related

BigQuery referencing subquery under WITH clause in WHERE clause

Basically I want to reference the ListOfIds subquery defined in the WITH clause directly as a single column table. As an example, I want to achieve something like the following.
WITH
ListOfIds AS (
SELECT
Id
FROM
...)
SELECT
*
FROM
...
WHERE
Id IN ListOfIds
The above syntax results in an error of ListOfIds is not defined on the line Id IN ListOfIds
So far the closest I can get to is the following, and I don't like it for its somewhat complicated and redundant syntax.
WITH
ListOfIds AS (
SELECT
Id
FROM
...)
SELECT
*
FROM
...
WHERE
Id IN (
SELECT
Id
FROM
ListOfIds)
Thanks in advance for any help or advice.
Firstly a note.
You probably don't want to do this.
CTEs (the WITH query) is a bit counterintuitive for people who normally code because it feels like a variable but it is not.
What actually happens is that you run the query many times to evaluate it, resulting in poor performance and extra $$ spent.
I recommend you replace this for a simple JOIN, it will achieve the same thing and generally be just way better.
Basically your query would be like:
WITH list_of_ids AS (
SELECT id FROM table_with_ids
)
SELECT main_table.*
FROM main_table
JOIN list_of_ids
ON main_table.id = list_of_ids.id
I think it is pretty clean syntax and solves your problem.
Let me know if there is something I am missing and I can add to this.
Addition to what Francesco says, if you really want to use it as a variable, actually it is also possible and probably not expensive at all.
DECLARE ListOfIds ARRAY<INT64> DEFAULT (SELECT ARRAY_AGG(Id) FROM ...);
SELECT
*
FROM
...
WHERE
Id IN UNNEST(ListOfIds)
Below is for BigQuery Standard SQL
#standardSQL
WITH ListOfIds AS (
SELECT ARRAY_AGG(Id) ids
FROM ...
)
SELECT * EXCEPT(ids)
FROM ...
,ListOfIds
WHERE id IN UNNEST(ListOfIds.ids)
[Super] Simplified example for testing/playing with above is :
#standardSQL
WITH ListOfIds AS (
SELECT ARRAY_AGG(Id) ids
FROM UNNEST([1, 2, 3, 4, 5]) id
)
SELECT * EXCEPT(ids)
FROM UNNEST([1, 3, 5, 7, 9]) id
,ListOfIds
WHERE id IN UNNEST(ids)
with output
Row id
1 1
2 3
3 5

Is it possible to use column values to determine the tablename in a from clause of a subselect?

I would like to run a query that uses a column value (in which a tablename is stored) to determine the tablename in a from clause of a subselect.
Something like that:
SELECT column_with_tablename,
(SELECT COUNT(*) FROM VALUEOF(column_with_tablename)) as
numberofitems
FROM table1
I know that this is very fragile but i need it work.
(I inherited a database that stores tablenames in a column)
Is there a way ?
for a pure sql solution, refer to this answer
select column_with_tablename,
to_number(extractvalue(xmltype
(dbms_xmlgen.getxml('select count(*) c from '||column_with_tablename)
),'/ROWSET/ROW/C')) as count
from table1

HQL Subquery Problems

I'm getting a HQL error every time I try to run this subquery. This type of query should work in SQL right? How is HQL handling this type of query differently?
SELECT *
FROM Table
WHERE user_Id IN (
SELECT a.user_Id
FROM Table a
WHERE a.color='Blue')
It looks like HQL does not support IN?
I'm getting a error: "Cannot recognize input near 'SELECT'"
In hql if you want to select all fields, you have two ways:
1-You can remove select clause like:
FROM Table
WHERE user_Id IN (
SELECT a.user_Id
FROM Table a
WHERE a.color='Blue')
or
2-If you want to use select clause, you must use an alias:
SELECT t
FROM Table t
WHERE t.user_Id IN (
SELECT a.user_Id
FROM Table a
WHERE a.color='Blue')
It looks like your syntax is incorrect. Try this:
SELECT *
FROM Table
WHERE user_Id IN (
SELECT a.user_Id
FROM Table a
WHERE a.color='Blue')
There is no "*" in HQL. Only alias should be present.
SELECT t
FROM Table t
HQL supports "In".
Also, make sure, the java class field name is "user_Id". You may be using the DB column name here.

Distinct with select *

Is it possible to use select * with distinct or write easily something that has the same impact?
I need to select all columns from a table with distinct value, but listing all the columns in select clause would be nerve-breaking because the number of columns is over 20!
In Microsoft SQL Server you can write:
select distinct * from MyTable
However, it is considered "best practice" to specify the columns explicitly, partly because it improves the performance of the query, but also to protect yourself from failures that would arise if the database schema were to change in the future
This should work:
SELECT DISTINCT * FROM TABLE_NAME
Use this query:
SELECT DISTINCT Employee, Rank
FROM Employees
Adding the "distinct" keyword right after "select" does the work.
For example:
SELECT DISTINCT * FROM TABLE_NAME

Oracle VPD how to reference the table

I'm struggling with the following problem. I want to restrict access to a table using Oracle VPD. In the where clause that the select policy function returns I need a reference to the table in a subquery as follows:
select * from Table t1
where not exists (select 1 from Table t2 where t2.name=t1.name and t2.date=t1.date)
(The example doesn't make a lot of sense except to illustrate the problem)
I have no control over the select part of the query.
select * from Table t1
The table can be given any alias or none at all. So therefore I have no idea how to reference the table in the where clause.
Is there a way to get around this?
(Oracle 10GR2)
I think you will need to use NOT IN:
(name, datecol) not in (select name, datecol from table2)