
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to add a column from a select query but the value from the new column will be the row count of the MySQL select query?
For this, you can use MySQL row_number(). Let us first create a table −
mysql> create table DemoTable1342 -> ( -> Score int -> ); Query OK, 0 rows affected (0.68 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1342 values(80); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable1342 values(98); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1342 values(78); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1342 values(89); Query OK, 1 row affected (0.07 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1342;
This will produce the following output −
+-------+ | Score | +-------+ | 80 | | 98 | | 78 | | 89 | +-------+ 4 rows in set (0.00 sec)
Following is the query to add a column from a select query but the value from the new column will be the row count of the MySQL select query −
mysql> select Score,row_number() over() as `Rank` from DemoTable1342 order by Score DESC;
This will produce the following output −
+-------+------+ | Score | Rank | +-------+------+ | 98 | 1 | | 89 | 2 | | 80 | 3 | | 78 | 4 | +-------+------+ 4 rows in set (0.32 sec)
- Related Questions & Answers
- MySQL SELECT to add a new column to a query and give it a value?
- MySQL query to select average from distinct column of table?
- Select the table name as a column in a UNION select query with MySQL?
- How to get a specific column record from SELECT query in MySQL?
- MySQL query to select the nth highest value in a column by skipping values
- MySQL query to select all the records only from a specific column of a table with multiple columns
- MySQL query to return the count of only NO values from corresponding column value
- Select minimum row value from a column with corresponding duplicate column values in MySQL
- How to select the maximum value of a column in MySQL?
- Select the minimum value from the maximum values of two tables with a single MySQL query?
- MySQL query to SELECT rows with LIKE and create new column containing the matched string?
- MySQL query to display the count of distinct records from a column with duplicate records
- A single MySQL query to select value from first table and insert in the second?
- MySQL query to fetch the maximum corresponding value from duplicate column values
- MySQL query to group by names and display the count in a new column
Advertisements