SQL - Problem with subquery in the FROM clause - sql

I am currently learning SQL and I tried testing something but it does not work.
The query that I tried is the following:
SELECT acc_id
FROM
(
SELECT *
FROM company
);
The inner query must return the whole table and the outter query must select from that table only a specific column. Simple as it seems this produces an error. The error message is:
"You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 6" (line 6 being the last line).
I can't figure out whats the issue.

You need to give an alias to your subquery:
SELECT acc_id
FROM
(
SELECT *
FROM company
) AS some_alias;
Although your query can be simplified into:
SELECT acc_id
FROM company;

Related

Getting Count Of select Query

My Need is to get the count of query that gets Executed. I got the Query to get Count Like
Select Count(*) from ( Select Query) from myTable
but the problem is when user enters a special character like the comma, period etc it shows error like Syntax error at or near ')'. but from the user point of view, there is no ')'.
How do solve this? Is there any other way to get the count. My final output should Syntax error at or near ',' or '.' etc if they are present in the Query
You need sub-query and for that you can use CTE(common table expression)
Select Count(*) from ( ) from alias-- your sql
this way you can do post sql server and postgre
with t1 as
(
Select Query ---- your sql code
)
select Count(*) from t1 --- count from alis

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 )

SQL Apache Derby - Select last value in column

I am using Apache Derby and am trying to select the last value in a column.
Currently I have the following:
SELECT id FROM hotels ORDER BY id DESC WHERE ROWNUM <=1;
However this is resulting in a syntax error:
Syntax error: Encountered "WHERE" at line 1, column 44.
Would anyone know the proper way to write this query?
Thank you.
The order by clause goes after the where. Perhaps you intend:
SELECT MAX(id)
FROM hotels;

How to get the latest value and the one right before that

I was reviewing this question/answer
Get latest date before date value in row
I thought I would give it a try, but every time I try the 'outer apply'
I get an error: Incorrect syntax near ')'.
In my query, my 'from' statement is not as simple as the example in the question & answer.
My 'from' has multiple joins and then at the every end of my joins I'm trying this:
outer apply
(
SELECT top 1 *
from <mytable>
where <mytable.column> in ('1','3')
)
Any additional help is much appreciated
I'm using SQLserver 2008 R2
Make sure you alias your outer apply. I don't know why SQL Server is always so unhelpful with this error.
outer apply
(
SELECT top 1 *
from <mytable>
where <mytable.column> in ('1','3')
) AS obligatory_alias
I would actually do something like the following
select ColumnsFromTableAYouCareAbout, max(b.datecolumn) as lastdate from mytable a
inner join mytable b on b.datecolumn < a.datecolumn
group by ColumnsFromTableAYouCareAbout

problem in select into query

Why m i getting error incorrect syntax near the keyword IN in the following query?
select * into persons_backup IN 'HRMS.mdb' from persons
Thank you
Assuming SQL Server (based on your previous question) you would need
select *
into persons_backup
from HRMS.mdb.persons
or
select *
into HRMS.mdb.persons_backup
from persons
dependant upon what you are trying to do exactly. See SELECT ... INTO syntax here
Assuming you want to add all rows from persons into another table persons_backup:
Insert into persons_backup select * from persons;
Depending on the RDBMS you use, you might have to put () around the select.