Wednesday 20 February 2013

SQL Select Command


SQL Select

SQL Select command is used to show columns and rows from tables.
SYNTAX:
SELECT * FROM NAMEOFTABLE;
EXAMPLE:
SELECT * FROM ITEMS


DESCRIPTION:
SELECT command can be used in many ways. One of the ways is to see all the columns of a table with all the data in it. “Select * from nameoftable” command is used to show all the data inside tables. Esteric(*) is for showing all columns inside a table


Select Columns

SELECT Command can also be used to see only specific columns from a table. For example, lets say our table name is items and inside table there are many columns but we only want to see ItemID, ItemName columns.
In that case the SQL Query will be
SYNTAX:
SELECT COLUMN1, COLUMN2 FROM NAMEOFTABLE
EXAMPLE:
Select ItemID, ItemName from items


Create Copy of table

It is possible to create a copy of a table using the following select statement:
Syntax:
Select * into NewTableName from TableName





SQL Where

SQL Where Clause is used to filter Data. For example if we have many products in our items table and we want to see only cars then we are going to use Where Clause in our SQL Query
SYNTAX:
SELECT * FROM TABLENAME WHERE COLUMNNAME = ‘VALUE’


EXAMPLE:
SELECT * FROM ITEMS WHERE ItemName = ‘cars’;
SELECT * FROM ITEMS_SHAHID4 WHERE ITEMNAME = ‘KEYBOARD’;


DESCRIPTION:
WHERE Clause is use to restrict the selection to a specified criteria. The above mentioned example will be used if we want to select only those items where name is car


SQL Wild Card
 
SQL Wild Card is used to search for a specific combination of characters. For example we want to see all those products from our items table where name starts with b. In this case we can't really filter data by only using WHERE, thats why we are going to use WildCard. SQL WildCard is represented by %


SYNTAX:
SELECT * FROM nameoftable WHERE column LIKE ‘%search words%’
EXAMPLE:
SELECT * FROM items WHERE itemName LIKE ‘%php%’;

Another Example:
SELECT * FROM items WHERE itemName LIKE 'b%';


DESCRIPTION:
Wild cards (%) are used to for searching something from tables. The keyword that is used with wild card (%) is LIKE. In the above mentioned example, our table name is items and we are searching for all the items where item names consist of word php. Wild cards can be used on many kinds of search pages on websites.

No comments:

Post a Comment