Background documentationSelecting, Arranging and Renaming Columns Locate this document in the navigation structure

 

You can select specific columns in the query and arrange and rename these columns in the results table.

Prerequisites

You can use the demo data for the SQL tutorial. Start the Database Studio as database administrator MONA with the password RED and log on to demo database DEMODB: Logging On to a Database.

Activities

You can use Database Studio to enter and execute SQL statements. More information: Working with SQL Statements: Overview

Selecting Specific Columns

To select columns from a table, specify the desired column name after the keyword SELECT. Use commas to separate the column names in this list. Blank characters are optional.

SELECT firstname, name

  FROM hotel.customer

Displaying the first and last names of all customers

Result

FIRSTNAME

NAME

Jenny

Porter

Peter

Brown

?

Datasoft

Rose

Brian

Mary

Griffith

Martin

Randolph

Sally

Smith

Mike

Jackson

Rita

Doe

George

Howe

Frank

Miller

Susan

Baker

Joseph

Peters

?

TOOLware

Antony

Jankins

Selecting All Columns

Instead of the list of column names, you can also enter *.

SELECT *

  FROM hotel.customer

The system displays the contents of the entire customer table. The columns are displayed in the order you defined when you created the table.

Arranging Columns

You can arrange the columns to be displayed in any order you desire. To do so, enter the column names in the desired order after the keyword SELECT.

SELECT name, firstname

  FROM hotel.customer

Displaying the last and first names of all customers

Result

NAME

FIRSTNAME

Porter

Jenny

Brown

Peter

Datasoft

?

Brian

Rose

Griffith

Mary

Randolph

Martin

Smith

Sally

Jackson

Mike

Doe

Rita

Howe

George

Miller

Frank

Baker

Susan

Peters

Joseph

TOOLware

?

Jankins

Antony

Renaming Columns

You can rename the columns to be displayed.

SELECT zip, name city_name

  FROM hotel.city

    WHERE state = 'NY'

Displaying the zip code and city/place name of all cities/places in the state of NY

Result

ZIP

CITY_NAME

10019

New York

10580

New York

11788

Long Island

12203

Albany