Wednesday, January 19, 2011

Selecting and Viewing Records

In the I Break Code’s Inserting Records post we inserted two authors into our Authors table and two books into our Books table, which reside in our Library database. After we insert this information into our database, we need some means of retrieving that information. We can do that with the SELECT statement.

The SELECT statement is fairy simple to use. We can use it to select specific columns, or all columns.

Let’s say we wanted to view all our records, or rows, in our Authors table. We would write it like this:

  1. USE Library;
  2. GO
  3.  
  4. SELECT * FROM Authors;

And SQL Server would return this output when executed:

author_ID

first_Name

last_Name

1

Geoffrey

Chaucer

2

William

Shakespeare

And if we wanted to select our books from our database we would do it like so:

  1. USE Library;
  2. GO
  3.  
  4. SELECT * FROM Books;

And SQL Server would return this output when executed:

book_ID

title

genre

description

1

The Cantebury Tales

Classic/Poetry

A great collection of poems and stories.

2

Macbeth

Classic/Play

"Fair is foul, and foul is fair". - ( Quote Act I, Scene I).

We can also specify exactly what columns we want to select from our tables too:

  1. USE Library;
  2. GO
  3.  
  4. SELECT first_Name, last_Name FROM Authors;
  5. SELECT title FROM Books;

And SQL Server would return this output:

Output from line 4:

first_Name

last_Name

Geoffrey

Chaucer

William

Shakespeare

Output from line 5:

title

The Cantebury Tales

Macbeth

Try  experimenting by selecting different combinations of column names, in different orders, or the same column names multiple times to see what kind of output you can come up with.

No comments:

Post a Comment

I Break Code Where code gets done.
ASP.NET | HTML | SQL Server | VB.NET | Request A Topic |