Query

Solo disponible en BuenasTareas
  • Páginas : 8 (1766 palabras )
  • Descarga(s) : 0
  • Publicado : 30 de septiembre de 2010
Leer documento completo
Vista previa del texto
SQL Structured Query Language
SQL SELECT Statement

The SQL SELECT Statement
The SELECT statement is used to select data from a table. The tabular result is stored in a result table (called the result-set).
Syntax
SELECT column_name(s)FROM table_name |
Note: SQL statements are not case sensitive. SELECT is the same as select.

SQL SELECT Example
To select the content of columns named"LastName" and "FirstName", from the database table called "Persons", use a SELECT statement like this:
SELECT LastName,FirstName FROM Persons |
The database table "Persons":
LastName | FirstName | Address | City |
Hansen | Ola | Timoteivn 10 | Sandnes |
Svendson | Tove | Borgvn 23 | Sandnes |
Pettersen | Kari | Storgt 20 | Stavanger |
The result
LastName | FirstName |
Hansen | Ola|
Svendson | Tove |
Pettersen | Kari |

Select All Columns
To select all columns from the "Persons" table, use a * symbol instead of column names, like this: 
SELECT * FROM Persons |
Result
LastName | FirstName | Address | City |
Hansen | Ola | Timoteivn 10 | Sandnes |
Svendson | Tove | Borgvn 23 | Sandnes |
Pettersen | Kari | Storgt 20 | Stavanger |

The SELECT DISTINCTStatement
The DISTINCT keyword is used to return only distinct (different) values.
The SELECT statement returns information from table columns. But what if we only want to select distinct elements?
With SQL, all we need to do is to add a DISTINCT keyword to the SELECT statement:
Syntax
SELECT DISTINCT column_name(s)FROM table_name |

Using the DISTINCT keyword
To select ALL values from thecolumn named "Company" we use a SELECT statement like this:
SELECT Company FROM Orders |
"Orders" table
Company | OrderNumber |
Sega | 3412 |
W3Schools | 2312 |
Trio | 4678 |
W3Schools | 6798 |
Result
Company |
Sega |
W3Schools |
Trio |
W3Schools |
Note that "W3Schools" is listed twice in the result-set.
To select only DIFFERENT values from the column named "Company" we use aSELECT DISTINCT statement like this:
SELECT DISTINCT Company FROM Orders |
Result:
Company |
Sega |
W3Schools |
Trio |

The WHERE Clause 
To conditionally select data from a table, a WHERE clause can be added to the SELECT statement.
Syntax
SELECT column FROM tableWHERE column operator value |
With the WHERE clause, the following operators can be used:
Operator | Description |= | Equal |
| Not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal |
1965This is wrong:SELECT * FROM Persons WHERE Year>'1965' |

The LIKE Condition
The LIKE condition is used to specify a search for a pattern in a column.
Syntax
SELECT column FROM tableWHERE column LIKE pattern |
A "%" sign can be used to define wildcards (missing letters in thepattern) both before and after the pattern.

Using LIKE
The following SQL statement will return persons with first names that start with an 'O':
SELECT * FROM PersonsWHERE FirstName LIKE 'O%' |
The following SQL statement will return persons with first names that end with an 'a':
SELECT * FROM PersonsWHERE FirstName LIKE '%a' |
The following SQL statement will return persons with first namesthat contain the pattern 'la':
SELECT * FROM PersonsWHERE FirstName LIKE '%la%' |

The INSERT INTO Statement
The INSERT INTO statement is used to insert new rows into a table.
Syntax
INSERT INTO table_nameVALUES (value1, value2,....) |
You can also specify the columns for which you want to insert data:
INSERT INTO table_name (column1, column2,...)VALUES (value1, value2,....) |

Insert aNew Row
This "Persons" table:
LastName | FirstName | Address | City |
Pettersen | Kari | Storgt 20 | Stavanger |
And this SQL statement:
INSERT INTO Persons VALUES ('Hetland', 'Camilla', 'Hagabakka 24', 'Sandnes') |
Will give this result:
LastName | FirstName | Address | City |
Pettersen | Kari | Storgt 20 | Stavanger |
Hetland | Camilla | Hagabakka 24 | Sandnes |

Insert Data...
tracking img