- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Selecting a single row in MySQL?
If you want to select a single row on the basis of primary key, use the WHERE clause. The syntax is as follows −
SELECT * FROM yourTableName WHERE yourPrimaryKeyColumnName = someValue;
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table selectWithPrimaryKey -> ( -> Id int NOT NULL AUTO_INCREMENT, -> Name varchar(20), -> Age int, -> Marks int, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.78 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into selectWithPrimaryKey(Name,Age,Marks) values('Larry',24,98); Query OK, 1 row affected (0.15 sec) mysql> insert into selectWithPrimaryKey(Name,Age,Marks) values('John',23,89); Query OK, 1 row affected (0.21 sec) mysql> insert into selectWithPrimaryKey(Name,Age,Marks) values('Mike',21,85); Query OK, 1 row affected (0.18 sec) mysql> insert into selectWithPrimaryKey(Name,Age,Marks) values('Sam',26,56); Query OK, 1 row affected (0.18 sec) mysql> insert into selectWithPrimaryKey(Name,Age,Marks) values('Carol',21,59); Query OK, 1 row affected (0.18 sec) mysql> insert into selectWithPrimaryKey(Name,Age,Marks) values('Bob',20,91); Query OK, 1 row affected (0.21 sec) mysql> insert into selectWithPrimaryKey(Name,Age,Marks) values('David',28,93); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from selectWithPrimaryKey;
The following is the output −
+----+-------+------+-------+ | Id | Name | Age | Marks | +----+-------+------+-------+ | 1 | Larry | 24 | 98 | | 2 | John | 23 | 89 | | 3 | Mike | 21 | 85 | | 4 | Sam | 26 | 56 | | 5 | Carol | 21 | 59 | | 6 | Bob | 20 | 91 | | 7 | David | 28 | 93 | +----+-------+------+-------+ 7 rows in set (0.00 sec)
The following is the query to select a single row from the table −
mysql> select *from selectWithPrimaryKey where Id = 6;
Here is the output −
+----+------+------+-------+ | Id | Name | Age | Marks | +----+------+------+-------+ | 6 | Bob | 20 | 91 | +----+------+------+-------+ 1 row in set (0.00 sec)
- Related Articles
- Sum values of a single row in MySQL?
- MySQL LIMIT to select a single row
- Update multiple columns of a single row MySQL?
- Selecting only a single field from MongoDB?
- Get the Average of Average in a single MySQL row?
- MySQL query to return all items in a single row
- Selecting a value in custom order from another column in a MySQL table with a single query
- Get only a single value from a specific MySQL row?
- Concatenate multiple rows and columns in a single row with MySQL
- Return only a single row from duplicate rows with MySQL
- How can we delete a single row from a MySQL table?
- Comparing two columns in a single MySQL query to get one row?
- Can we update a row with the highest ID in a single MySQL query?
- Display all the column values in a single row separated by comma in MySQL?
- Creating and Selecting a MySQL Database

Advertisements