SQL Apache Derby - Select last value in column - sql

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;

Related

SQL - Problem with subquery in the FROM clause

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;

SQL GROUP BY 1 2 3 and SQL Order of Execution

This may be a dumb question but I am really confused. So according to the SQL Query Order of Execution, the GROUP BY clause will be executed before the SELECT clause. However it allows to do something like:
SELECT field_1, SUM(field_2) FROM myTable GROUP BY 1
My confusion is that if GROUP BY clause happens before SELECT, in this scenario I provided, how does SQL know what 1 is? It works with ORDER BY clause and it makes sense to me because ORDER BY clause happens after SELECT.
Can someone help me out? Thanks in advance!
https://www.periscopedata.com/blog/sql-query-order-of-operations
My understanding is because it's ordinal notation and for the SELECT statement to pass syntax validation you have to have at least selected a column. So the 1 is stating the first column in the select statement since it knows you have a column selected.
EDIT:
I see people saying you can't use ordinal notation and they are right if you're using SQL Server. You can use it in MySQL though.
select a,b,c from emp group by 1,2,3. First it will group by column a then b and c. It works based on the column after the select statement.
Each GROUP BY expression must contain at least one column that is not an outer reference. You cannot group by 1 if it is not a column in your table.

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

TSQL Distinct, and OrderBy and WHERE

I have the following SQL statement sample:
SELECT DISTINCT [CommentNdx]
,[CommentsText]
,[DateTimeAdded]
FROM [dbo].[CommentTable]
ORDER BY [dbo].[CommentTable].DateTimeStart DESC
WHERE [CommentsText] = 'Hello World'
I keep getting the error Incorrect syntax near the keyword 'WHERE'. I know the syntax is incorrect but I'm not sure how this should be formatted. Any help is appreciated.
UPDATE:
My mistake, I meant date time start should be datetimeadded. Corrected syntax.
SELECT DISTINCT [TestCommentNdx]
,[TestID]
,[CommentsText]
,[DateTimeAdded]
,[OperatorNdx]
FROM [PTDB].[dbo].[TestsComments]
WHERE [TestID] = 1174411854
ORDER BY [PTDB].[dbo].[TestsComments].[DateTimeAdded] DESC
UPDATE 2:
Thanks much everyone, one last thing, would it make a difference if there were joins in the select statement? I have a really long query with joins and when I try to use DISTINCT, I get ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
The WHERE needs to come before the ORDER BY. Also, you won't be able to sort by DateTimeStart unless it's included in the SELECT statement.
SELECT DISTINCT [CommentNdx]
,[CommentsText]
,[DateTimeAdded]
FROM [dbo].[CommentTable]
WHERE [CommentsText] = 'Hello World'
ORDER BY [dbo].[CommentTable].DateTimeStart DESC -- you can't do this
ORDER BY follows the WHERE clause.
EDIT: As per #LukeGirvin post, you can't sort by a column that isn't included in the SELECT clause.

Simple SELECT query problem

I have this MySql select query which seems to have a bug but I am quite "green" so I simply cannot see, so maybe you could help?
Here is the query:
SELECT node_id
FROM rate
WHERE node_id='".$cat_node_id_string."'
LIMIT ".$node_count_star.",".$node_count_end."
ORDER BY SUM(amount)
GROUP BY node_id
Thanks for help in advance...
UPDATE:
I will post a mysql error to make it clearer...
You have an error in your SQL syntax;
check the manual that corresponds to
your MySQL server version for the
right syntax to use near 'GROUP BY
node_id LIMIT 1,20' at line 5
try this
SELECT node_id
FROM rate
WHERE node_id='".$cat_node_id_string."'
ORDER BY SUM(amount)
GROUP BY node_id
LIMIT ".$node_count_star.",".$node_count_end."
Be aware though, that the result will be a single record containing whatever $cat_node_id_string resolves to!
With WHERE node_id='".$cat_node_id_string."' you tell MySQL to only return those records where node_id matches an exact string.
With GROUP BY node_id you tell MySQL to group all records into one
Your error:
GROUP BY node_id LIMIT ,'
That comma there suggests that your limit variables $node_count_star and $node_count_end are empty.
Cheers & good luck.