SQL SELECT Query

SQL SELECT query

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.

idnameclassmark
1John DeoFour75
2Max RuinThree85
3ArnoldThree55
4Krish StarFour60
SQL does not guarantee the order of returned rows unless you use ORDER BY.
Full student table with SQL Dump SELECT Query Exercises

SQL SELECT Syntax Top ↑

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;
nameclass
John DeoFour
Max RuinThree
ArnoldThree
Krish StarFour

Select Only the Columns You Need Top ↑

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.

Using SELECT * Top ↑

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.

Column Aliases and Expressions Top ↑

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.

Return Unique Values with DISTINCT Top ↑

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.

SQL DISTINCT Tutorial

Filter Rows with WHERE Top ↑

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.

Sort and Limit SELECT Results Top ↑

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;
idnamemark
33Kenn Rein96
12Recky94
32Binn Rott90

For pagination, LIMIT is normally combined with a deterministic ORDER BY so moving between pages does not depend on an unspecified row order.

Common SELECT Clause Order Top ↑

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.

Display SELECT Results with PHP PDO Top ↑

SQL SELECT query using PHP PDO

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 PDO

SQL SELECT Video Tutorials Top ↑

SELECT query with LIMIT, ORDER BY and WHERE conditions

20 SELECT queries using WHERE, BETWEEN, AND, OR, IN and LIKE

Common SQL SELECT Problems Top ↑

Assuming Rows Have a Default Order Top ↑

A table has no guaranteed presentation order. Add ORDER BY whenever the result must be consistently sorted.

Using SELECT * Everywhere Top ↑

SELECT * is useful for exploration, but application queries are often clearer and more efficient when they request only the required columns.

Writing Clauses in the Wrong Order Top ↑

For example, WHERE is written before ORDER BY and LIMIT. Follow the normal SELECT clause order shown above.

Using = NULL in a WHERE Condition Top ↑

NULL is tested with IS NULL or IS NOT NULL, not ordinary equality. See the SQL NULL tutorial.

Building SQL with Untrusted Application Input Top ↑

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.

Selecting More Data Than the Page Needs Top ↑

Large tables and large columns can make unnecessary result sets expensive. Select the required columns and apply appropriate filtering or limits for the task.

SQL Introduction WHERE Condition ORDER BY

LIMIT DISTINCT Linking Tables

Copy Table Data LEFT JOIN UNION

Frequently Asked Questions Top ↑

Q1: What does SQL SELECT do?

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.

Q2: What is the difference between SELECT * and selecting column names?

SELECT * returns every column in the result. Listing column names returns only the fields requested and usually makes application queries clearer.

Q3: Does SELECT return rows in insertion order?

No order should be assumed unless the query contains ORDER BY. If a stable order matters, specify the sort columns explicitly.

Q4: How do I return only some rows with SELECT?

Use WHERE to filter rows by conditions. In MySQL, LIMIT can also restrict how many rows are returned.

Q5: How do I return unique values with SELECT?

Use DISTINCT, such as SELECT DISTINCT class FROM student. DISTINCT removes duplicate result combinations from the selected columns.

Q6: Can SELECT contain calculations or functions?

Yes. The SELECT list can contain expressions and SQL functions, and AS can assign aliases to the resulting columns.

Q7: Should PHP use prepared statements for every SELECT?

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.



Full Student Table with SQL Dump Practice SELECT Queries

SQL Introduction WHERE Condition


Subscribe to our YouTube Channel here



plus2net.com
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?




SQL Video Tutorials










We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer