List of Topics

SfC Home > Web Design > SQL >

Retrieving Columns of Data with SQL

by Ron Kurtus (revised 17 March 2016)

You can use SQL to retrieve the columns of a database table with the SELECT statement.

You can retrieve all columns, a single column, or several specific columns. It is then up to your programming language to display that data.

Questions you may have include:

This lesson will answer those questions.



Retrieve all columns

You can query a database to retrieve all the elements in a database table by using the SELECT statement and wildcard (*) indicator.

SELECT *
FROM table_name;

Note: It is standard practice to add ";" at the end of your SQL query. Some databases require it and some don't.

Example

SELECT *
FROM parts_information;

resulting in:

id part part_no cost
1 hood 03-060 500
2 fender 07-340 750
3 bumper 05-021 135

Retrieve single column

The most common query from a database is to collect or retrieve all the elements in a specific column of the database table.

SELECT column_name
FROM table_name;

Example

SELECT part
FROM parts_information
;

resulting in:

part
hood
fender
bumper

Retrieve several select columns

You can also retrieve data from several columns by separating the column names with a comma.

SELECT column_1, column_2, column_7
FROM table_name;

Note: Do not place a comma after the last item in the list, because it will result in an error message.

Example

SELECT part, cost
FROM parts_information;

yields:

part cost
hood 500
fender 750
bumper 135

Summary

The SELECT statement is used to retrieve the columns of a database table. You can retrieve all columns, a single column, or several specific columns.


Use good tools


Resources and references

Ron Kurtus' Credentials

Websites

SQL Resources

Books

(Notice: The School for Champions may earn commissions from book purchases)

Top-rated books on Using SQL


Students and researchers

The Web address of this page is:
www.school-for-champions.com/sql/
retrieving_columns.htm

Please include it as a link on your website or as a reference in your report, document, or thesis.

Copyright © Restrictions


Where are you now?

School for Champions

SQL topics

Retrieving Columns of Data with SQL




SQL topics

Also see



Let's make the world a better place

Be the best that you can be.

Use your knowledge and skills to help others succeed.

Don't be wasteful; protect our environment.

You CAN influence the world.





Live Your Life as a Champion:

Take care of your health

Seek knowledge and gain skills

Do excellent work

Be valuable to others

Have utmost character

Be a Champion!



The School for Champions helps you become the type of person who can be called a Champion.