DBHow to Querying Data From table

How to Querying Data From table

This article teaches how to query for data in MySQL.

Querying Data

In this article, we’ll learn how to query the data we have stored in a table. The SELECT statement allows us to retrieve data from tables.

Example Syntax for Querying Data

SELECT col1, col2, … coln
FROM table
WHERE <condition>

Queries for this article

-- Query 1
SELECT * from Actors;
-- Query 2
SELECT <columns> FROM <TableName>
-- Query 3
SELECT FirstName, SecondName from Actors;
-- Query 4
SELECT FirstName, SecondName from Actors WHERE FirstName="Travolta";
-- Query 5
SELECT FirstName, SecondName from Actors WHERE FirstName="Brad";
-- Query 6
SELECT FirstName, SecondName from Actors WHERE NetWorthInMillions > 500;
-- Query 7
SELECT FirstName, SecondName from Actors WHERE NetWorthInMillions > 0;

SELECT * from Actors

Execute the following SELECT statement to retrieve all the rows in the table with all the columns.

SELECT * from Actors;

The command reads all the data in the table. The syntax for a simple SELECT command is as follows:

SELECT <columns> FROM <TableName>

The SELECT keyword is followed by a comma-separated list of columns we wish to display. Using an * displays all the columns. The table to query is specified using the FROM keyword followed by the table name.

SELECT FirstName, SecondName from Actors

In the next command, we’ll display the first name and second name columns. Execute the following command:

SELECT FirstName, SecondName from Actors;

The outcome is as follows:

The columns are displayed in the same order as they appear in the MYSQL query.

We can filter the rows for a select query using the WHERE clause. The WHERE clause specifies a criterion that rows must match to be returned by the SELECT query. The criteria may be met by zero, one, multiple, or all rows.
Executing the following query will result in no row being matched:

SELECT FirstName, SecondName from Actors WHERE FirstName="Travolt
a";

Executing the following query will result in exactly one row being matched:

SELECT FirstName, SecondName from Actors WHERE FirstName="Brad";

Executing the following query results in multiple rows being matched:

SELECT FirstName, SecondName from Actors WHERE NetWorthInMillion
s > 500;

Finally, executing the following query results in all rows being matched and returned:


SELECT FirstName, SecondName from Actors WHERE NetWorthInMillions > 0;

The following table captures the various operators that can be used in a WHERE clause.

OperatorPurpose
>Greater than operator
>=Greater than or equal to
operator
< Less than operator
<=Less than or equal to operator
!=Not equal operator
<>Not equal operator
<=>NULL-safe equal to operator
=Equal to operator
BETWEEN … AND …Whether a value is within a
range of values
COALESCE()Return the first non-NULL
argument
GREATEST() Return the largest argument
INWhether a value is within a set
of values
INTERVALReturn the index of the
argument that is less than the
first argument
ISTest a value against a boolean
IS NOTTest a value against a boolean
IS NOT NULLNOT NULL value test
IS NULLNULL value test
ISNULL()Test whether the argument is
NULL
LEAST() Return the smallest argument
LIKESimple pattern matching
NOT BETWEEN … AND …Whether a value is not within
a range of values
NOT IN()Whether a value is not within
a set of values

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe Today

GET EXCLUSIVE FULL ACCESS TO PREMIUM CONTENT

Get unlimited access to our EXCLUSIVE Content and our archive of subscriber stories.

Exclusive content

Latest article

More article