Saturday, March 12, 2016

SQL SELECT

SQL SELECT Command

SQL Select Command Video tutorial

What do we use SQL commands for? A common use is to select data from the tables located in a database. Immediately, we see two keywords: we need to SELECT information FROM a table. (Note that a table is a container that resides in the database where the data is stored. For more information about how to manipulate tables, go to the Table Manipulation Section). Hence we have the most basic SQL query structure:

SELECT "column_name" FROM "table_name";

There are three ways we can retrieve data from a table:
  • Retrieve one column 
  • Retrieve multiple columns 
  • Retrieve all columns 
Let's use the following table to illustrate all three cases:

Table Store_Information
Store_NameSalesTxn_Date
Los Angeles1500Jan-05-1999
San Diego250Jan-07-1999
Los Angeles300Jan-08-1999
Boston700Jan-08-1999

Select One Column
To select a single column, we specify the column name between SELECT and FROM as follows:

SELECT Store_Name FROM Store_Information;

Result:
Store_Name
Los Angeles
San Diego
Los Angeles
Boston
Select Multiple Columns

We can use the SELECT statement to retrieve more than one column. To select Store_Name and Sales columns from Store_Information, we use the following SQL:

SELECT Store_Name, Sales FROM Store_Information;

Result:
Store_Name   Sales
Los Angeles1500
San Diego250
Los Angeles300
Boston700

Select All Columns

There are two ways to select all columns from a table. The first is to list the column name of each column. The second, and the easier, way is to use the symbol *. For example, to select all columns from Store_Information, we issue the following SQL:

SELECT * FROM Store_Information;

Result:
Store_NameSalesTxn_Date
Los Angeles1500Jan-05-1999
San Diego250Jan-07-1999
Los Angeles300Jan-08-1999
Boston700Jan-08-1999

Exercises
For these exercises, assume we have a table called Users with the following columns:

Table Users
Column Name
First_Name
Last_Name
Birth_Date
Gender
Date_Joined

1. Which of the following SQL statement is incorrect? (There can be more than one answer) 
a) SELECT * FROM Users; 
b) SELECT First_Name, Gender, Last_Name FROM Users; 
c) SELECT First_Name, Last_Name Users; 
d) SELECT All FROM Users;

2. (True Or False) In SQL, the order of the columns in a SELECT statement must be the same as the order of the columns in the underlying table. For example, in the table Users, you must select First_Name before Last_Name.

3. (True Or False) The following two SQL statements are equivalent: 
a) Select * From Users; 
b) SELECT * FROM Users;

Answer
1. c), d).
2. False. The order of columns in a table has no relationship to the order of columns in a SELECT statement.
3. False. SQL keywords such as SELECT and FROM are not case-sensitive. Table names and column names, on the other hand, can be configured to be case-sensitive or case-insensitive depending on the database being used.

0 comments:

Post a Comment

 
Animated Social Gadget - Blogger And Wordpress Tips