The SQL SELECT statement retrieves data from a table. List the columns you need after SELECT and the table after FROM.
SELECT id, name, class, mark
FROM student;
This returns the four selected columns for every row in the student table.
| id | name | class | mark |
|---|---|---|---|
| 1 | John Deo | Four | 75 |
| 2 | Max Ruin | Three | 85 |
| 3 | Arnold | Three | 55 |
| 4 | Krish Star | Four | 60 |
SELECT column1, column2
FROM table_name;
Use a comma to separate multiple column names. The semicolon ends the SQL statement in tools and scripts that support multiple statements.
For example:
SELECT name, class
FROM student;
| name | class |
|---|---|
| John Deo | Four |
| Max Ruin | Three |
| Arnold | Three |
| Krish Star | Four |
When an application needs only a few fields, name those columns explicitly:
SELECT id, name, mark
FROM student;
This is usually clearer than retrieving every column. It also avoids transferring columns the application does not use.
An asterisk means all columns:
SELECT *
FROM student;
SELECT * is convenient while exploring a small table or when every column is genuinely needed. In application code, explicit column lists are often preferable because the required result structure remains clear even if the table later gains more columns.
Use AS to give a result column a readable alias:
SELECT name AS student_name, mark AS score
FROM student;
A SELECT list can also contain expressions and functions. For example, CONCAT() can combine values:
SELECT CONCAT(name, ' - ', class) AS student_details
FROM student;
The alias changes the result-column label; it does not rename the column in the table.
To return each class value once:
SELECT DISTINCT class
FROM student;
DISTINCT applies to the selected result values. Multiple selected columns make each unique combination significant.
SELECT returns all matching rows unless a condition restricts them. Use WHERE to filter the rows:
SELECT id, name, class, mark
FROM student
WHERE class = 'Four';
Other dedicated filtering tutorials cover LIKE, BETWEEN, IN, AND/OR and NULL conditions.
Use ORDER BY when the order matters:
SELECT id, name, mark
FROM student
ORDER BY mark DESC, id ASC;
Here the highest marks appear first. The additional id ASC provides a consistent tie-breaker when students have the same mark.
Use LIMIT in MySQL to return only part of the result:
SELECT id, name, mark
FROM student
ORDER BY mark DESC, id ASC
LIMIT 3;
| id | name | mark |
|---|---|---|
| 33 | Kenn Rein | 96 |
| 12 | Recky | 94 |
| 32 | Binn Rott | 90 |
For pagination, LIMIT is normally combined with a deterministic ORDER BY so moving between pages does not depend on an unspecified row order.
A more complete SELECT query can contain several clauses. They are written in this order:
SELECT column_list
FROM table_name
WHERE condition
GROUP BY column_list
HAVING group_condition
ORDER BY column_list
LIMIT row_count;
Not every SELECT query needs every clause. Use only the parts required by the result.
After creating a PDO database connection, a fixed SELECT query can be executed with query(). Select only the columns needed by the output.
<?php
require 'config.php';
$sql="SELECT id,name,class,mark
FROM student
ORDER BY id
LIMIT 4";
$stmt=$dbo->query($sql);
echo '<table>';
echo '<tr><th>ID</th><th>Name</th><th>Class</th><th>Mark</th></tr>';
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
$id=(int)$row['id'];
$name=htmlspecialchars((string)$row['name'],ENT_QUOTES,'Windows-1252');
$class=htmlspecialchars((string)$row['class'],ENT_QUOTES,'Windows-1252');
$mark=(int)$row['mark'];
echo "<tr><td>$id</td><td>$name</td><td>$class</td><td>$mark</td></tr>";
}
echo '</table>';
If the query includes external values such as a user-selected class or ID, use a PDO prepared statement instead of concatenating the value into SQL.
Fetch Multiple Records with PDOA table has no guaranteed presentation order. Add ORDER BY whenever the result must be consistently sorted.
SELECT * is useful for exploration, but application queries are often clearer and more efficient when they request only the required columns.
For example, WHERE is written before ORDER BY and LIMIT. Follow the normal SELECT clause order shown above.
NULL is tested with IS NULL or IS NOT NULL, not ordinary equality. See the SQL NULL tutorial.
SELECT syntax does not protect an application from SQL injection. When values come from a form, URL or other external source, validate them and use prepared statements in the application layer.
Large tables and large columns can make unnecessary result sets expensive. Select the required columns and apply appropriate filtering or limits for the task.
SELECT retrieves data from one or more database tables. You can choose specific columns and combine SELECT with clauses such as WHERE, ORDER BY and LIMIT.
SELECT * returns every column in the result. Listing column names returns only the fields requested and usually makes application queries clearer.
No order should be assumed unless the query contains ORDER BY. If a stable order matters, specify the sort columns explicitly.
Use WHERE to filter rows by conditions. In MySQL, LIMIT can also restrict how many rows are returned.
Use DISTINCT, such as SELECT DISTINCT class FROM student. DISTINCT removes duplicate result combinations from the selected columns.
Yes. The SELECT list can contain expressions and SQL functions, and AS can assign aliases to the resulting columns.
A fixed SELECT statement with no external values can be executed directly. When user or external values are included in conditions, use prepared statements and parameter binding.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| raju | 11-04-2013 |
| How to get ( select ) data from more than one table ? | |
| ranj | 01-01-2014 |
| how to get 200 characters/letters from a long description and to be followed by "..." | |
| midhu | 09-03-2015 |
| how to display data in database as table by some limit(first 10,first 20,all) using php | |
| smo | 09-03-2015 |
| You can display by using above code or to display first 10 you can use LIMIT query. By adding Order by to the query you can display in the order of highest to lowest or alphabetically or in any other combination. | |
| Daniel | 08-07-2015 |
| smo:::How to get ( select ) data from more than one table ? | |
| smo1234 | 09-07-2015 |
| To select data from different tables, you have to link them. Here is the tutorial on how to link more than one table.. | |
| Vivek Kumar Tyagi | 12-09-2015 |
| in last example .. what is $dbo?? | |
| smo1234 | 12-09-2015 |
| It is declared inside config.php file where all database connection details are kept. The link is there also. | |
01-03-2022 | |
| how the the student record will increase by adding 50% in the schoolarship any student whose cgpa >=3.00 | |
08-01-2023 | |
| You have to use update table query, add one where condition to filter cgpa UPDATE table_name set schoolarship=1.5*schoolarship WHERE cgpa >=3.00 | |
28-02-2024 | |
| Which of the following SQL clauses is used to filter records and include only those that fulfill a specified condition? | |