SQL SELECT TOP statement is not working properly - mysql

I am trying to view the first 2 records of a table name Customers which have two columns name Name(varchar) and Salary(text) in MySQL server 6.0
The command which I am using is:
SELECT TOP 2 * FROM customers;
But it's not working.

I recommend you to write statement like this using LIMIT
SELECT * FROM customers [WHERE conditions] [ORDER BY expression [ASC|DESC]] LIMIT 2 ;
Instead of
SELECT TOP 2 * FROM customers;

It's very simple. Just try this
SELECT * FROM customers LIMIT 2;
You can check the manual also.

Use this in case of MySQL:
select * from customers limit 2;

To get any position value in the whole tale I suggest you to use the following query as TOP [position] doesn't work in all versions of MySQL.
select min(column_name) from (select column_name from table_name order by column_name desc limit position_you_sort) any_name_for_table;

Related

In MariaDB how do I select the top 10 rows from a table?

I just read online that MariaDB (which SQLZoo uses), is based on MySQL. So I thought that I can use ROW_NUMBER() function
However, when I try this function in SQLZoo :
SELECT * FROM (
SELECT * FROM route
) TEST7
WHERE ROW_NUMBER() < 10
then I get this error :
Error: FUNCTION gisq.ROW_NUMBER does not exist
You can use the limit clause:
SELECT * FROM route LIMIT 10
This can, of course, be used on a sorted query too:
SELECT * FROM route ORDER BY some_field LIMIT 10
use LIMIT 10 at the end of your statement.
See the MySQL SELECT documentation.

counting rows of selected data in mysql

I'm going to count returned rows by this query:
select * from tableName where( number > 1000);
I tried this query to get rows count:
select count(*) as count from (select * from tableName where( number > 1000));
but I got syntax error. What is wrong with that?
You dont want Nested Query Just Use
select count(*) as count from tableName where number > 1000 ;
This works if you are using nested query & don't use 'count' as your temporary variable name:
select count(temp.id) as num from (select * from tableName where number > 1000) temp
It should be like this
select count(*) as noOfCount from tableName where number > 1000;
Do not use sql reserved keywords as your temp variable names
Why not counting the rows in 1 select directly:
select count(*) from tableName where number>1000;
Make it simple. Don't need a subquery
select count(*) as count from tableName where number > 1000;
Likely, the syntax error your are getting is "every derived table must have an alias".
To fix that syntax error, you would just assign an alias to the inline view query.
SELECT foo FROM (SELECT foo FROM bar) a ;
^
But for your specific query, an inline view isn't required.
You could simply modify your original query, to replace the * in the SELECT list with an aggregate expression such as COUNT(*). You can also assign an alias to the aggregate expression.
It's valid to use COUNT as a column alias, but my preference would be to use a different alias, one that isn't the name of a MySQL function.
FIrst thing first
SELECT COUNT(*) as tot_rows FROM `tableName` WHERE `number` > 1000 ;
you better give back tilt (`) sign arond number, cause might be there's a keyword in mysql called number. I am not pretty sure, but you should take precaution.
Second you can do another thing also.
If you are using PHP then
$query_result = mysql_query("SELECT * FROM `tableName` WHERE `number` > 1000");
$num_row = mysql_numrows($query_result);
This query works fine
select count(*) as count from tableName where number > 1000 ;
but FYR in ur query error is u don't define subquery name in example its tbl
select count(*) as count from (select * from tableName where( number > 1000)) as tbl;

SYNTAX CREATE VIEW SQL SERVER

i make a view in sql server and throw an error:
USE BaseDeDatos;
CREATE VIEW TEMAS_USUARIO
AS
SELECT TOP 5 t.id_userTopic, t.nameTopic, u.id_user, u.name
FROM Topic t, Users u
WHERE t.id_userTopic = u.id_group
ORDER BY t.id_topic DESC;
what is wrong whit the syntax?
error ---> CREATE VIEW TEMAS_USUARIO
canĀ“t use LIMIT
USE BaseDeDatos
GO
CREATE VIEW TEMAS_USUARIO
AS
SELECT TOP 5 t.id_userTopic, t.nameTopic, u.id_user, u.name
FROM Topic t, Users u
WHERE t.id_userTopic = u.id_group
ORDER BY t.id_topic DESC;
The SQL SELECT TOP Clause:
SQL SERVER / MS ACCESS Syntax
SELECT TOP number|percent column_name(s)
FROM table_name;
MySQL Syntax
SELECT column_name(s)
FROM table_name
LIMIT number;
SQL SELECT TOP Clause explanation: Here
The syntax command of CREATE VIEW you have this:
The SELECT clauses in a view definition cannot include the following:
An ORDER BY clause, unless there is also a TOP clause in the select list of the SELECT statement
Important note Important
The ORDER BY clause is used only to determine the rows that are returned by the TOP or OFFSET clause in the view definition. The ORDER BY clause does not guarantee ordered results when the view is queried, unless ORDER BY is also specified in the query itself.

How to select one record from a SELECT query in MySQL

I am running the follwoing SQL query:
SELECT * FROM COUNTRIES WHERE USERTYPE = 'PREMIUM' ORDER BY SALES DESC;
The output of this query yields a list of countries.
Now I need to populate a field in another table which is like TOP_SALES_COUNTRY, SECOND_TOP_SALES_COUNTRY and THRID_TOP_SALES_COUNTRY for which I only need the first,second and third records in the output of this SELECT statement. Kindly advise on how this can be achieved.
SELECT * FROM COUNTRIES WHERE USERTYPE = 'PREMIUM' ORDER BY SALES DESC limit 0,1; to get the first row
What worked for me is a modification of reverse_engineer's answer. FOr example to get 2nd top most country i used LIMIT 1,1 and for third value I used LIMIT 2,1. Thank you for your help. Immensely grateful to all.
You could use any of the following:
Option 1:
SELECT TOP number|percent column_name(s)
FROM table_name;
Option 2:
SELECT column_name(s)
FROM table_name
LIMIT number;
Option 3:
SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;

SQL Query using Random

Hi could anyone help me with something?
I need a mysql query that can return 4 values from a column in some table, also select all fiels from each row.
something like:
SELECT * FROM dadoslivros WHERE RAND() =1 limit 100;
But i only want the random form row ID.
Thanks.
It seems like you just want to use ORDER BY RAND():
select *
from dadoslivros
order by rand()
limit 100
See SQL Fiddle with Demo