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:
- USE Library;
- GO
-
- 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:
- USE Library;
- GO
-
- 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:
- USE Library;
- GO
-
- SELECT first_Name, last_Name FROM Authors;
- 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