Yogesh Chauhan's Blog

SQL Basics: What does the asterisk (*) mean in a query?

in SQL/MySQL on December 12, 2021

The character asterisk (*) is used often in SQL queries. You can say it’s very handy and special.

The asterisk (*) is used in conjunction with the SELECT keyword to retrieve data from all columns from a table.

To understand how useful it is, let’s assume that we have a database table users with column names userid, username, firstname, lastname, job_title.

If you want to display all users with every piece of information you have to write this query:


select userid, username, firstname, lastname, job_title 
from users

This is relatively easy because we only have 6 columns but in real life, you might have hundred or a few hundred columns depending on your data. So, in that case, to get all those data from each and every column is a time consuming task. But the asterisk (*) makes it easy!

Retrieving All Rows and Columns from a Table

Here’s how you can get all columns and rows using asterisk (*) with SELECT.


select * 
from users

You don’t need to worry about column names when you use asterisk to retrieve data.

Get all or specific columns?

But again, everything depends on the data you want to retrieve as well. If you don’t want to get all the columns and just want to get specific columns only then you are better off with writing names of those columns in an SQL query.

Another advantage of getting specific columns is that if you’re retrieving the data to pass it to another program, you’d want to make sure you’re passing the specific columns that were supposed to be passed. In that case if you use asterisk (*) and pass all the columns, you might get an error depending on the program structure.

Use it with WHERE

WHERE clause if used for retrieving a subset of rows from a table.

If you want to limit the number of rows you get in your results, you’d want to use WHERE or HAVING clause.

For e.g if you just want to get one row, you can use this query assuming your userid column has all unique values in it.


select * 
from users
WHERE userid = 1


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to convert datetime to date format in JavaScript?JavaScriptHow to get the first element with a class name xyz using JavaScript?JavaScriptJavaScript Data Types and Data Structures Things to RememberJavaScriptSelf-Driving and Intelligent NetworksMiscellaneousHow SCSS (Sass) finds a module without a file extension?SCSSPHP Login System using PDO Part 1: Create User Registration PagePHP