ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

SQL Select Statement

Updated on December 4, 2012

The SQL Select Statement is the most commonly used, and perhaps, one of the very first SQL commands one should learn to use.

Rather than start off with explanations and definitions, I'm jumping right in with the creation of a very simple table, and then giving you several examples of SQL Select commands and the results provided.

In essence, I'm going to show you specific "SELECT" examples first, and then follow up with some brief syntax commonly used.

By showing you the simple table structure, I'm hoping that will help you clearly understand the SQL "Select" statement commands and query results.

NOTE - In this article, I'll be using Microsoft's SSMS (SQL Server Management Studio) 2008R2

Grab that energy drink and let's get started!

Creating a Very Simple SQL Table

In my hub "How to Back up a SQL 2005 Database", I created a database named "Howlermunkey INC". Now I'll show you how to easily create a very simple table which I will then use to query the "Select" statement against.

  1. First I'll open SSMS and expand my "Howlermunkey INC" database
  2. I'll then right click "Tables" and select "New Table"
  3. I'll name my first column "HUB_NAME", with the data type as "nvarchar(MAX)"
  4. I'll name my second column "DATE_PUB" with the data type as "datetime"
  5. Next I'll save the table as "HUBS"
  6. Then I'll quickly populate my new table "HUBS" by right clicking the table and selecting "Edit Top 200 Rows"
  7. Finally I'll add the Names of my hubs published in the first column, then the date published in the second , TIP - (12/4/12) format works just fine.
  8. Now I'll close that query window and create a "New Query".

Create\ing a simple 2 Column Table
Create\ing a simple 2 Column Table
RIGHT CLICK AND EDIT TOP 200 ROWS
RIGHT CLICK AND EDIT TOP 200 ROWS

SQL Select Statements

~

1. SELECT * from HUBS (shows the entire "hubs" table)

2. SELECT * from HUBS ORDER BY DATE_PUB (shows entire table and sorts by date)

3. SELECT HUB_NAME from HUBS (shows only the column "HUB_NAMES")

~

SELECT * from HUBS (Select ALL from table)
SELECT * from HUBS (Select ALL from table)
Select * from HUBS Order by Date_PUB
Select * from HUBS Order by Date_PUB
SELECT HUB_NAME from HUBS (Select column from table)
SELECT HUB_NAME from HUBS (Select column from table)

SQL SELECT, WHERE, LIKE

`

4. SELECT * from HUBS where Date_Pub > ('2012/11/20') (only hubs published after 11/20)

5. SELECT * from HUBS where HUB_NAME like (%ubuntu%) (Shows any hub with "ubuntu" in "HUB_Name" column)

TIP - You may have noticed I'm using the same SSMS query window, you can highlight the command you need and hit query, you will get results only from the command(s) highlighted

SQL SELECT WHERE, LIKE, AND

`

6. SELECT * from HUBS where HUB_NAME like (%ubuntu%) AND Date_Pub > ('2012/11/11')

SQL SELECT Examples

  • SELECT * FROM Table
  • SELECT * FROM Table ORDER BY expression
  • SELECT * FROM Table LIKE expression AND expression
  • SELECT Column FROM Table Order By order_expression

Have a general Idea? That was my intention! There are plenty of resources available where you can dig deep into SQL Statement syntax. My goal here was to show you a very basic table, with very common "SELECT" queries, giving you very practical results, and helping you learn FAST!


Comments

    0 of 8192 characters used
    Post Comment

    • jimagain profile image

      jimagain 4 years ago from Hattiesburg, Mississippi

      Loved this. It seemed simple enough even I can do it. I use Excel a lot. Made me want to get the software so I could use it. Does Excel or Access have similar features?

    • howlermunkey profile image
      Author

      Jeff Boettner 4 years ago from Tampa, FL

      @jimagain, glad to hear your response, that was my goal! Excel can do some things similar, but has a worksheet/workbook type layout. Excel is good for certain projects. Access is a simplified version of DB MGMT, but is space limited. For a small project access could suffice. The beauty of SQL is the dynamic abilities (and speed).

      For example, picture hundreds of tables in one db linked together, connected to .net and ported through a web server.

      But sql is actually fun, very useful, and I do recommend spending some time with it if you like what you see. Thanks for stopping by!

      PS -- you can download Microsoft's SQL express for free :)

    • jimagain profile image

      jimagain 4 years ago from Hattiesburg, Mississippi

      Thanks. I plan on downloading the SQL Epress. I can afford free.

    • howlermunkey profile image
      Author

      Jeff Boettner 4 years ago from Tampa, FL

      @jimagain, haha, yup, I'm all about free too. Hit me up if you need version info etc.

    • dwachira profile image

      [ Danson Wachira ] 4 years ago from Nairobi, Kenya

      Hi howlermunkey,

      Ever since i fell in love with SQL during Visual FoxPro programming, i have always liked the queries, simple yet powerful. Once one grab the concept of "SELECT ....WHERE" the other concepts become more understandable. I like how you have done it simple here. Great article, voted up and shared with followers.

    Click to Rate This Article