SELECT P.pID FROM Department D, Professor P
WHERE D.dID = P.dID
ORDER count(pID);
Mysql, keeps throwing an error 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 'count(pID)
LIMIT 0, 30' at line 3
ORDER needs to be ORDER BY.
You are missing a BY.
The query should be:
SELECT P.pID FROM Department D, Professor P
WHERE D.dID = P.dID
ORDER BY count(pID);
Related
This is the code I was given by my professor:
SELECT fName, lName, NoOfMovies
FROM Directors D, (
SELECT dID, COUNT(*) NoOfMovies
FROM MovieDirectors
GROUP BY dID
HAVING COUNT(*) > =500) AS T
WHERE D.dID = T.dID
ORDER BY NoOfMovies;
The error code for sql is "#1064 - 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 'LIMIT 0, 25' at line 2."
We are actually just supposed to be learning indexes. Her code is supposed to be correct, yet when I let her know that she has an error, she will not help. I have attempted to rewrite the code:
Select fName, lName, NoOfMovies
From Directors D Join moviedirectors MD on (D.dID=MD.dID)
GROUP by D.dID
Having COUNT(*) >=500 As T
Where D.dID =MD.dID
ORDER by NoOfMovies;
Yet, I am still getting an error that I am not using the correct syntax near the variable T.My error code is: "#1064 - 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 'As T
Where D.dID =MD.dID
ORDER by NoOfMovies LIMIT 0, 25' at line 4." I am running this in phpmyadmin. If anyone can help, it would be greatly appreciated.
The code should probably look like this:
select d.fName, d.lName, COUNT(*) as NoOfMovies
from Directors d join
moviedirectors md
on d.dID = md.dID
group by d.fName, d.lName
having count(*) >= 500
order by NoOfMovies;
Notes:
Qualify all the column names with the name of the table they are coming from.
Always use explicit, proper JOIN syntax.
A WHERE clause would go right after the FROM, but the clause is not necessary.
The columns in the GROUP BY should match the unaggregated columns in the SELECT.
I think your rewrite is actually very close, but I would use this slight modification:
SELECT
d.ID,
d.fName,
d.lName,
COUNT(*) AS NoOfMovies
FROM Directors d
INNER JOIN moviedirectors md
ON d.dID = md.dID
GROUP BY
d.dID, d.fName, d.lName
HAVING
COUNT(*) >= 500;
ORDER BY
NoOfMovies;
The only major change here is the removal of the WHERE clause, which no longer serves a purpose. It is no longer needed because that logic has become part of the ON clause. I also group the first and last name, in addition to the ID. If the ID uniquely determines the first and last name this may not be necessary, but I did it just to be safe.
I have a SQL query like this:
SELECT E.snum
FROM Enrolled E
GROUP BY E.snum
HAVING COUNT (*) >= ALL (SELECT COUNT(*)
FROM enrolled E2
GROUP BY E2.snum)
But it generates an error:
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 ') >= ALL (SELECT COUNT() FROM enrolled E2 GROUP BY E2.snum)
LIMIT 0, 30' at line 1
What does the error mean? Why it doesn't allow me to use ALL operator?
Try this
SELECT E.snum, COUNT(E.snum)
FROM Enrolled E
GROUP BY E.snum
HAVING COUNT (E.snum) >= ALL (SELECT COUNT(*)
FROM enrolled E2
GROUP BY E2.snum)
My query:
SELECT id, pl, ka, user, users.imie, users.id, users.nazwisko, users.klasa
FROM word
LEFT JOIN users ON word.user = users.id BY word.pl ASC
It gives me this error:
#1064 - 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
'BY word.pl ASC LIMIT 0, 30' at line 1
Missing Order in Order By
ALSO Both tables have an id column, so you need to call either/both users.id and word.id to avoid the ambiguous error:
SELECT users.id, word.id, pl, ka, user, users.imie, users.id, users.nazwisko, users.klasa
FROM word
LEFT JOIN users ON word.user = users.id
ORDER BY word.pl ASC
You forgot ORDER:
ORDER BY word.pl ASC
Add Order by clause
SELECT id, pl, ka, user, users.imie, users.id, users.nazwisko, users.klasa
FROM word
LEFT JOIN users ON word.user = users.id
ORDER BY word.pl ASC
Can anyone help me with this sql statement?
SELECT c.id AS 'cat_id', c.name, sol.id,
(SELECT f.id
FROM folders f, category_folder_mapping cf
WHERE cf.folder_id = f.id AND cf.category_id = c.id
ORDER BY f.id ASC LIMIT 1) AS 'folder_id'
FROM categories c
OUTER APPLY folderFirstSolution(folder_id) sol
ORDER by c.id ASC
My goal in this sql is to use the 'folder_id' as parameter for the folderFirstSolution() function. I always get an sql syntax error.
Please tell me what's wrong with this statement.
Thanks ahead.
(EDIT)
MYSQL ERROR:
1064 - 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 'OUTER APPLY folderFirstSolution(folder_id) sol
ORDER by c.id ASC
LIMIT 0, 25' at line 7
I am getting a syntax error can anybody tell me why?
SELECT c.clientid, c.clientname, c.billingdate,
(SELECT TOP 1 previousbalance FROM invoice i
WHERE i.client = c.clientid ORDER BY i.invoiceid DESC) AS remaining
FROM client c
ORDER BY clientname
What the secondary select is doing is getting the latest record for that clientid in the invoice table.
The program - HediSQl
SQL
And here is the error:
SQL Error (1064): 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 '1 previousbalance FROM invoice i WHERE i.client = c.clientid ORDER BY i.invoicei' at line 1 */
Just guessing but it might indicate that you should replace TOP 1 with LIMIT 1 or WHERE ROWNUM < 2 LIMIT 1. What kind of DB are you using?
You just need to use LIMIT instead TOP like this:
SELECT c.clientid, c.clientname, c.billingdate,
(SELECT previousbalance FROM invoice i
WHERE i.client = c.clientid ORDER BY i.invoiceid DESC LIMIT 1) AS remaining
FROM client c
ORDER BY clientname
See this SQLFiddle
The syntax you have will work in SQL Server. Your error message is from MySQL.
Try this:
SELECT c.clientid,
c.clientname,
c.billingdate,
(
SELECT previousbalance
FROM invoice i
WHERE i.client = c.clientid
ORDER BY i.invoiceid DESC
LIMIT 1
) AS remaining
FROM client c
ORDER BY clientname