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 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#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 #Jan 14 How to display a student’s individual transcript in Colleague? #Jan 11 How to install PuTTY on a MacOS?
You might also like these
How to create a Star Ratings using CSS?CSS4 ways to create Date Objects in JavaScriptJavaScriptA simple example on grid ‘auto-fill’ vs ‘auto-fit’CSSIf statement shorthand examples in JavaScriptJavaScriptLearn how to give a temporary name to a column or to a table using SQL AliasesSQL/MySQLWhat happens when we add Numbers and Strings in JavaScript?JavaScript