strasbal strasbal - 19 days ago 5
SQL Question

SELECT mulitple parameters on mySQL Query

I'm trying to learn to use mySQL by going through the W3's tutorials. I was reading about the SQL BETWEEN operator and how it allows you to select values within a range. I was following the tutorial example:

SELECT * FROM Products
WHERE ProductName BETWEEN 'C' AND 'M';


and thought it would be cool if you could also sort by ProductName in Ascending order. I've tried:

SELECT * FROM Products
WHERE ProductName BETWEEN 'C' AND 'M' AND OrderBY ASC;


and

SELECT * FROM Products
WHERE ProductName BETWEEN 'C' AND 'M';
AND OrderBY ASC;


as well as

SELECT * FROM Products
WHERE ProductName BETWEEN 'C' AND 'M', OrderBY ASC;


Each one gives an error:

'Syntax error (missing operator) in query expression'


But I'm not sure of the proper syntax when using multiple parameters. More generally I was wondering if an SQL query can have multiple parameters. i.e. Select this table and sort according to parameter one, and parameter two or if you would need to create a separate query for each action.

Answer

This is the correct syntax:

SELECT *
FROM Products
WHERE ProductName BETWEEN 'C' AND 'M'  -- no comma/semicolon after [WHERE] clause
ORDER BY ProductName ASC;              -- ORDER BY colName..