HQL Subquery Problems - sql

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.

Related

Query to select all IDs from a table that are non-existing IDs in the reference table?

SELECT [USER_ID]
FROM [DB].[dbo].[PhoneNumbers]
WHERE NOT EXISTS (SELECT [USER_ID] FROM [DB].[dbo].[Users])
I am trying something like this, but I got stuck in the SQL server syntax.
I need to select all rows from the PhoneNumbers table that have User_IDs that DO NOT exist in the Users table so I can delete orphaned data.
Please try the below query
SELECT [USER_ID]
FROM [DB].[dbo].[PhoneNumbers]
WHERE [USER_ID] NOT IN (SELECT [USER_ID] FROM [DB].[dbo].[Users])
If you need to enclose a keyword as a table name in SQL Server, use [], and in MySQL use backticks.
Try Removing square braces and replace with backticks.
Your query simply need a correlation clause:
SELECT [USER_ID]
FROM [DB].[dbo].[PhoneNumbers] p
WHERE NOT EXISTS (SELECT 1 FROM [DB].[dbo].[Users] U WHERE u[USER_ID] = p.[USER_ID]);
I strongly, strongly recommend using NOT EXISTS instead of NOT IN with a subquery. NOT IN does not behave as expected if the subquery returns any NULL values; in that case, no rows are ever returned.

Oracle subquery after like error

I have the following SQL query
SELECT * FROM users u WHERE 1=1 and u.user_name LIKE 'A%'
It works as expected. But the following line results in an error message.
SELECT * FROM users u WHERE 1=1 and u.user_name LIKE (select '%arthur%' from dual)
The error message is the following:
ORA-00933 SQL Command not properly ended
I have tried to close the query with ";" but it is still gives the same error. What could cause this error?
Edit: I need LIKE and can't use IN, lets assume we have only one 'arthur' in the users database.
Your query appears to be correct syntactically. The subquery is a scalar subquery, which returns only one column and at most one row.
In general, you can use LIKE with a subquery using EXISTS:
SELECT u.*
FROM users u
WHERE 1 = 1 AND
EXISTS (SELECT 1
FROM t
WHERE u.user_name LIKE t.col
);
If your actual query is more complicated, then this might solve your actual problem.
i have tried same query as per your,it worked fine.
SELECT * FROM employee e where 1=1 and e.name like (select '%abc%' from dual);

DB2 SELECT EXCEPT with WHERE clause

I'm trying to compare two tables in a DB2 database in z/OS using SPUFI to submit SQL queries.
I'm doing this by using EXCEPT to see the difference between two SELECT queries.
I need to filter the SELECT statement from the first query with a WHERE clause.
SELECT KEY_FIELD_1,LOOKUP_FIELD_1
FROM TABLE_1
WHERE FILTER_FIELD = '1'
EXCEPT
SELECT KEY FIELD_2,LOOKUP_FIELD_2
FROM TABLE_2
I got results back, but it also returned an error -199 Is this because the WHERE clause is not present in the second SELECT statement?
ERROR: ILLEGAL USE OF KEYWORD EXCEPT.
TOKEN <ERR_STMT> <WNG_STMT> GET SQL
SAVEPOINT HOLD FREE ASSOCIATE WAS EXPECTED
Try introducing parentheses e.g.
( SELECT KEY_FIELD_1,LOOKUP_FIELD_1
FROM TABLE_1
WHERE FILTER_FIELD = '1' )
EXCEPT
( SELECT KEY FIELD_2,LOOKUP_FIELD_2
FROM TABLE_2 )

Can where clause condition be represented by another column

I have users enter the condistions for where clause for a table. Now I want to use that clause to do select. How can i do that? Example,
Condition table ( ckey, condition)
1 fn like 'G%' and ln like 'B%'
Name table (nkey, fn, ln)
Query wanted
select * from Name where ... use condition in row 1 of Condition table .....
Most DBMSes support a subquery inside WHERE, including correlated subquery.
It's hard to say without more info, but you'll probably need a (correlated?) subquery on Condition table.
One possible option would be to setup your condition table to mimic the table you're searching on. Then you could simply join the tables on all fields. In order for this to work you would have to default all columns in the condition table to '%' in case that condition isn't specified.
If your requirements are too complex for that to work, then another solution would be to use a stored procedure that generates dynamic sql based on what's in the condition table.
See below
accept myID prompt "Enter ID : "
SELECT * FROM myTable WHERE id='&myID'
OR
SELECT * FROM myTable WHERE id= #Enter_ID)
see which one is working with you...

Subselect in pgSQL

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.