SQL Commands

  • Published on
    31-Dec-2015

  • View
    15

  • Download
    0

Embed Size (px)

DESCRIPTION

Pocket guide about SQL Commands

Transcript

<ul><li><p> 1 page of [20] </p><p>BASIC SQL COMMANDS ( Structured Query Language ) </p></li><li><p> 2 page of [20] </p><p>CONTENTS </p><p>1. SQL DATABASE TABLE ...................................................................................... 03 </p><p>2. SQL SELECT............................................................................................................ 03 </p><p>3. SQL SELECT INTO ................................................................................................ 03 </p><p>4. SQL DISTINCT ........................................................................................................ 04 </p><p>5. SQL WHERE ............................................................................................................ 04-05 </p><p>6. SQL LIKE .................................................................................................................. 06 </p><p>7. SQL INSERT INTO ................................................................................................. 07-08 </p><p>8. SQL UPDATE ........................................................................................................... 08 </p><p>9. SQL DELETE ........................................................................................................... 09 </p><p>10. SQL ORDER BY ...................................................................................................... 09-10 </p><p>11. SQL OR &amp; AND ....................................................................................................... 10-11 </p><p>12. SQL IN ....................................................................................................................... 11-12 </p><p>13. SQL BETWEEN ....................................................................................................... 12-13 </p><p>14. SQL ALIASES .......................................................................................................... 13 </p><p>15. SQL COUNT ............................................................................................................. 13-14 </p><p>16. SQL MAX .................................................................................................................. 14 </p><p>17. SQL MIN ................................................................................................................... 14 </p><p>18. SQL AVG ................................................................................................................... 14-15 </p><p>19. SQL SUM ................................................................................................................... 15 </p><p>20. SQL GROUP BY ...................................................................................................... 16-17 </p><p>21. SQL HAVING ........................................................................................................... 17 </p><p>22. SQL JOIN .................................................................................................................. 18-20 </p></li><li><p> 3 page of [20] </p><p>SQL Commands </p><p> SQL Database Table </p><p>Table: Customers </p><p>FirstName LastName Email DOB Phone </p><p>John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222 </p><p>Steven Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545 </p><p>Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232 </p><p>James Smith jim@supergig.co.uk 20/10/1980 416 323-8888 </p><p>SELECT </p><p>The SQL SELECT statement is used to select data from a SQL database table. </p><p>Please have a look at the general SQL SELECT syntax: </p><p>SELECT Column1, Column2, Column3, </p><p>FROM Table1 </p><p>The list of column names after the SQL SELECT command determines which columns you want to </p><p>be returned in your result set. </p><p>SELECT * </p><p>FROM Table1 </p><p>When the list of columns following the SELECT SQL command is replaced with asterix (*) all table </p><p>columns are returned. </p><p>SELECT INTO </p><p>The SQL SELECT INTO statement is used to select data from a SQL database table and to insert it </p><p>to a different table at the same time. </p><p>SELECT Column1, Column2, Column3, </p><p>INTO Table2 </p><p>FROM Table1 </p><p>If we want to make an exact copy of the data in our Customers table, we need the following SQL </p><p>SELECT INTO statement: </p><p>SELECT * </p><p>INTO Customers_copy </p><p>FROM Customers </p></li><li><p> 4 page of [20] </p><p>DISTINCT </p><p>The SQL DISTINCT clause is used together with the SQL SELECT keyword, to return a dataset </p><p>with unique entries for certain database table column. </p><p>We will use our Customers database table to illustrate the usage of SQL DISTINCT. </p><p> FirstName LastName Email DOB Phone </p><p>John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222 </p><p>Steven Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545 </p><p>Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232 </p><p>James Smith jim@supergig.co.uk 20/10/1980 416 323-8888 </p><p>For example if we want to select all distinct surnames from our Customers table, we will use the </p><p>following SQL DISTINCT statement: </p><p>SELECT DISTINCT LastName </p><p>FROM Customers </p><p>LastName </p><p>Smith </p><p>Goldfish </p><p>Brown </p><p>WHERE </p><p>The SQL WHERE clause is used to select data conditionally, by adding it to already existing SQL </p><p>SELECT query. We are going to use the Customers table from the previous chapter, to illustrate the </p><p>use of the SQL WHERE command. </p><p>Table: Customers </p><p>FirstName LastName Email DOB Phone </p><p>John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222 </p><p>Steven Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545 </p><p>Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232 </p><p>James Smith jim@supergig.co.uk 20/10/1980 416 323-8888 </p><p>If we want to select all customers from our database table, having last name 'Smith' we need to use the </p><p>following SQL syntax: </p><p>SELECT * </p><p>FROM Customers </p><p>WHERE LastName = 'Smith' </p><p>The result of the SQL expression above will be the following: </p><p>FirstName LastName Email DOB Phone </p><p>John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222 </p><p>James Smith jim@supergig.co.uk 20/10/1980 416 323-8888 </p></li><li><p> 5 page of [20] </p><p>In this simple SQL query we used the "=" (Equal) operator in our WHERE criteria: </p><p>LastName = 'Smith' </p><p>But we can use any of the following comparison operators in conjunction with the SQL WHERE </p><p>clause: </p><p> (Not Equal) </p><p>SELECT * </p><p>FROM Customers </p><p>WHERE LastName 'Smith' </p><p>&gt; (Greater than) </p><p>SELECT * </p><p>FROM Customers </p><p>WHERE DOB &gt; '1/1/1970' </p><p>&gt;= (Greater or Equal) </p><p>SELECT * </p><p>FROM Customers </p><p>WHERE DOB &gt;= '1/1/1970' </p><p>&lt; (Less than) </p><p>SELECT * </p><p>FROM Customers </p><p>WHERE DOB &lt; '1/1/1970' </p></li><li><p> 6 page of [20] </p><p>LIKE </p><p>We will use the Customers table to illustrate the SQL LIKE clause usage: </p><p> FirstName LastName Email DOB Phone </p><p>John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222 </p><p>Steven Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545 </p><p>Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232 </p><p>James Smith jim@supergig.co.uk 20/10/1980 416 323-8888 </p><p>The SQL LIKE clause is very useful when you want to specify a search condition within your SQL </p><p>WHERE clause, based on a part of a column contents. For example if you want to select all customers </p><p>having FirstName starting with 'J' you need to use the following SQL statement: </p><p>SELECT * </p><p>FROM Customers </p><p>WHERE FirstName LIKE 'J%' </p><p>Here is the result of the SQL statement above: </p><p> FirstName LastName Email DOB Phone </p><p>John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222 </p><p>James Smith jim@supergig.co.uk 20/10/1980 416 323-8888 </p><p>If you want to select all Customers with phone numbers starting with '416' you will use this SQL </p><p>expression: </p><p>SELECT * </p><p>FROM Customers </p><p>WHERE Phone LIKE '416%' </p><p>The '%' is a so called wildcard character and represents any string in our pattern. </p><p>You can put the wildcard anywhere in the string following the SQL LIKE clause and you can put as </p><p>many wildcards as you like too. </p><p>Note that different databases use different characters as wildcard characters, for example '%' is a </p><p>wildcard character for MS SQL Server representing any string, and '*' is the corresponding wildcard </p><p>character used in MS Access. </p><p>Another wildcard character is '_' representing any single character. </p><p>The '[]' specifies a range of characters. Have a look at the following SQL statement: </p><p>SELECT * </p><p>FROM Customers </p><p>WHERE Phone LIKE '[4-6]_6%' </p></li><li><p> 7 page of [20] </p><p>This SQL expression will return all customers satisfying the following conditions: </p><p> The Phone column starts with a digit between 4 and 6 ([4-6]) </p><p> Second character in the Phone column can be anything (_) </p><p> The third character in the Phone column is 6 (6) </p><p> The remainder of the Phone column can be any character string (%) </p><p> FirstName LastName Email DOB Phone </p><p>John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222 </p><p>Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232 </p><p>James Smith jim@supergig.co.uk 20/10/1980 416 323-8888 </p><p>INSERT INTO </p><p>The SQL INSERT INTO syntax has 2 main forms and the result of either of them is adding a new </p><p>row into the database table. </p><p>The first syntax form of the INSERT INTO SQL clause doesn't specify the column names where the </p><p>data will be inserted, but just their values: </p><p>INSERT INTO Table1 </p><p>VALUES (value1, value2, value3) </p><p>The second form of the SQL INSERT INTO command, specifies both the columns and the values to </p><p>be inserted in them: </p><p>INSERT INTO Table1 (Column1, Column2, Column3) VALUES (Value1, Value2, Value3) </p><p>As you might already have guessed, the number of the columns in the second INSERT INTO syntax </p><p>form must match the number of values into the SQL statement; otherwise you will get an error. </p><p>If we want to insert a new row into our Customers table, we are going to use one of the following 2 </p><p>SQL statements: </p><p>INSERT INTO Customers </p><p>VALUES ('Peter', 'Hunt', 'peter.hunt@tgmail.net', '1/1/1974', '626 888-8888') </p><p>INSERT INTO Customers (FirstName, LastName, Email, DOB, Phone) </p><p>VALUES ('Peter', 'Hunt', 'peter.hunt@tgmail.net', '1/1/1974', '626 888-8888') </p><p>The result of the execution of either of the 2 INSERT INTO SQL statements will be a new row added </p><p>to our Customers database table: </p><p> FirstName LastName Email DOB Phone </p><p>John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222 </p><p>Steven Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545 </p><p>Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232 </p><p>James Smith jim@supergig.co.uk 20/10/1980 416 323-8888 </p><p>Peter Hunt peter.hunt@tgmail.net 1/1/1974 626 888-8888 </p></li><li><p> 8 page of [20] </p><p>If you want to enter data for just a few of the table columns, youll have to use the second syntax form of the SQL INSERT INTO clause, because the first form will produce an error if you havent </p><p>supplied values for all columns. </p><p>To insert only the FirstName and LastName columns, execute the following SQL statement: </p><p>INSERT INTO Customers (FirstName, LastName) </p><p>VALUES ('Peter', 'Hunt') </p><p>UPDATE </p><p>UPDATE Table1 </p><p>SET Column1 = Value1, Column2 = Value2 </p><p>WHERE Some_Column = Some_Value </p><p>The SQL UPDATE clause changes the data in already existing database row(s) and usually we need </p><p>to add a conditional SQL WHERE clause to our SQL UPDATE statement in order to specify which </p><p>row(s) we intend to update. </p><p>If we want to update the Mr. Steven Goldfish's date of birth to '5/10/1974' in our Customers database </p><p>table </p><p> FirstName LastName Email DOB Phone </p><p>John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222 </p><p>Steven Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545 </p><p>Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232 </p><p>James Smith jim@supergig.co.uk 20/10/1980 416 323-8888 </p><p>we need the following SQL UPDATE statement: </p><p>UPDATE Customers </p><p>SET DOB = '5/10/1974' </p><p>WHERE LastName = 'Goldfish' AND FirstName = 'Steven' </p><p>If we dont specify a WHERE clause in the SQL expression above, all customers' DOB will be updated to '5/10/1974', so be careful with the SQL UPDATE command usage. </p><p>We can update several database table rows at once, by using the SQL WHERE clause in our </p><p>UPDATE statement. For example if we want to change the phone number for all customers with last </p><p>name Smith, we need to use the following SQL UPDATE statement: </p><p>UPDATE Customers </p><p>SET Phone = '626 555-5555' </p><p>WHERE LastName = 'Smith' </p><p>After the execution of the UPDATE SQL expression above, the Customers table will look as follows: </p><p> FirstName LastName Email DOB Phone </p><p>John Smith John.Smith@yahoo.com 2/4/1968 626 555-5555 </p><p>Steven Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545 </p><p>Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232 </p><p>James Smith jim@supergig.co.uk 20/10/1980 626 555-5555 </p></li><li><p> 9 page of [20] </p><p>DELETE </p><p>So far weve learnt how to select data from a database table and how to insert and update data into a database table. Now its time to learn how to remove data from a database. Here comes the SQL </p><p>DELETE statement! </p><p>The SQL DELETE command has the following generic SQL syntax: </p><p>DELETE FROM Table1 </p><p>WHERE Some_Column = Some_Value </p><p>If you skip the SQL WHERE clause when executing SQL DELETE expression, then all the data in </p><p>the specified table will be deleted. The following SQL statement will delete all the data from our </p><p>Customers table and well end up with completely empty table: </p><p>DELETE FROM Table1 </p><p>If you specify a WHERE clause in your SQL DELETE statement, only the table rows satisfying the </p><p>WHERE criteria will be deleted: </p><p>DELETE FROM Customers </p><p>WHERE LastName = 'Smith' </p><p>The SQL query above will delete all database rows having LastName 'Smith' and will leave the </p><p>Customers table in the following state: </p><p>FirstName LastName Email DOB Phone </p><p>Steven Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545 </p><p>Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232 </p><p>ORDER BY </p><p>The SQL ORDER BY clause comes in handy when you want to sort your SQL result sets by some </p><p>column(s). For example if you want to select all the persons from the already familiar Customers table </p><p>and order the result by date of birth, you will use the following statement: </p><p>SELECT * FROM Customers </p><p>ORDER BY DOB </p><p>The result of the above SQL expression will be the following </p><p> FirstName LastName Email DOB Phone </p><p>John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222 </p><p>Steven Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545 </p><p>Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232 </p><p>James Smith jim@supergig.co.uk 20/10/1980 416 323-8888 </p><p>As you can see the rows are sorted in ascending order by the DOB column, but what if you want to </p><p>sort them in descending order? To do that you will have to add the DESC SQL keyword after your </p><p>SQL ORDER BY clause </p></li><li><p> 10 page of [20] </p><p>SELECT * FROM Customers </p><p>ORDER BY DOB DESC </p><p>The result of the SQL query above will look like this: </p><p> FirstName LastName Email DOB Phone </p><p>James Smith jim@supergig.co.uk 20/10/1980 416 323-8888 </p><p>Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232 </p><p>Steven Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545 </p><p>John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222 </p><p>If you don't specify how to order your rows, alphabetically or reverse, than the result set is ordered </p><p>alphabetically, hence the following to SQL expressions produce the same result: </p><p>SELECT * FROM Customers </p><p>ORDER BY DOB </p><p>SELECT * FROM Customers </p><p>ORDER BY DOB ASC </p><p>You can sort your result set by more than one column by specifying those columns in the SQL </p><p>ORDER BY list. The following SQL expression will order by DOB and LastName: </p><p>SELECT * FROM Custom...</p></li></ul>