SELECT * FROM to Fetch data from a MySQL Database Table

How to Fetch data from a MySQL Database Table?

SQL SELECT * FROM

Use the SELECT statement to fetch data from a table of a MySQL database.

Syntax of the SELECT statement

SELECT column_names FROM table_name;
  • table_name – Name of the table whose data you want to get.
  • column_names – Specify the column names (column1, column2, column3, …) that will be listed in the fetched data. Add * instead of the column names if you want to fetch all the columns.

Example of SELECT statement

  • Database Name – my_test_db
  • Table Name – users
  • users table columns – name, age, email

data of the `users` table

users table data

1. Get all data with all columns

The following both SQL statements will do the same job. Get all the data from the users table.

SELECT name, age, email FROM users;
SELECT * FROM users;
users table data

2. Get all data with specific columns

The following SQL statement will skip the email column. Don’t specify the column name If you don’t want the data of that column.

SELECT name, age FROM users;
The email column is skipped.

3. SELECT * FROM with WHERE clause

If you want to fetch only those rows that fulfill your condition, you can do this with the help of the WHERE clause.

Example:

In the following image of a SQL table, there is a list of users. Now, you want to get only those users whose age is below 18. Let’s see how you can implement this –

dummy users table

In the following SQL code, the < is a comparison operator. age < 18 means select those users whose age is less than 18.

SELECT * FROM users WHERE age < 18;
SQL WHERE age is below 18