SQL Select Statement
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.
- First I'll open SSMS and expand my "Howlermunkey INC" database
- I'll then right click "Tables" and select "New Table"
- I'll name my first column "HUB_NAME", with the data type as "nvarchar(MAX)"
- I'll name my second column "DATE_PUB" with the data type as "datetime"
- Next I'll save the table as "HUBS"
- Then I'll quickly populate my new table "HUBS" by right clicking the table and selecting "Edit Top 200 Rows"
- 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.
- Now I'll close that query window and create a "New Query".
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")
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!