Click here to load reader

SQL Tutorial Ref Pl-SQL

  • View

  • Download

Embed Size (px)

Text of SQL Tutorial Ref Pl-SQL

SQL TutorialWhat is SQL?

SQL stands for Structured Query Language SQL is pronounced as /s.kjul/ SQL is designed for manipulate data in relational database management system

What can you do with SQL?

SQL can retrieve and update data from relational database management system by using data manipulation language. SQL can create and modify database schema and database objects such as tables, views, indexes... via data definition language. SQL can grant or revoke authorization to user through data control language.

SQL Data Manipulation Language (DML)SQL data manipulation language (DML) is a main part of SQL statements. SQL DML allows you to retrieve data from the database tables in relational database management system (RDBMS). In addition, SQL DML allows you change or delete data from the database tables by using UPDATE and DELETE statements. Here are all the SQL statements related to the SQL data manipulation language: 1. SQL SELECT 2. SQL WHERE 3. SQL ALIAS 4. SQL DISTINCT 5. SQL ORDER BY 6. SQL IN 7. SQL BETWEEN 8. SQL LIKE 9. SQL GROUP BY 10. SQL HAVING 11. SQL INNER JOIN 12. SQL OUTER JOIN 13. SQL SELF-JOIN 14. SQL SUBQUERIES 15. SQL UNION 16. SQL INSERT 17. SQL UPDATE 18. SQL DELETE

SQL Data Definition Language (DDL)SQL Data definition language is a subset of SQL statements. SQL Database definition language allows yout to create, alter or remove different kind database objects such as tables, views, indexes.


2. SQL ALTER TABLE 3. SQL DROP TABLE SQL Tutorial References(for Practical)

SQL Sample DatabaseWe use Northwind database as the sample database through all SQL tutorials in our website. The Northwind sample database provides you a good database structure and sample data to allow you to experiment with SQL statements you learn in each tutorial. We also try to make the tutorial as common as possible so you can practice with various Relational Database Management Systems (RDBMS). The sample database is also available in almost common RDBMSs such as MS Access, MS SQL Server, MySQL. Here is the database diagram of the Northwind sample database:



Summary: In this tutorial, you will learn how to use SQL SELECT statement to retrieve data from a database table. The SQL SELECT statement allows you to retrieve the data from one or more database table. Here is the common syntax of the SQL SELECT statement:1 2 3 4 5 6 SELECT column_list FROM table_list WHERE row_conditions GROUP BY column_list HAVING group_conditions ORDER BY sort_list ASC | DESC

First you have to specify the table name that you want to retrieve the data. The table name is followed by the keyword FROM. Second you have to indicate what data in want to retrieve. You can list the specify the column of the database table after the keyword SELECT. Suppose you want to retrieve last name of employees in the employees table, you can perform the following query:1 SELECT lastname 2 FROM employees

The query fetches the values from the column lastname in all rows in the employees table and returns the result below.

You can also use SQL SELECT statement to retrieve multiple columns from a database table. In order to do so you must provide a comma between columns in the column list. For example, if you want to retrieve first name, last name and title of all employees, you can execute the following query:1 SELECT lastname, firstname, title 2 FROM employees


To retrieve all information of employee in the employees table, you can list all columns name and separate them by commas. In case a database table having many columns and you do not want to list them all after the keyword SELECT, you can use an asterisk (*) to indicate that you want to retrieve all columns without explicitly specifying them in the column list. For instance, the following query allows you to retrieve all employee's information by using the asterisk (*):1 SELECT * 2 FROM employees

In this tutorial, youve learned how to use simple SQL SELECT statement to retrieve data from a table. Youve also learned how to select a single column, multiple columns by separating them with a comma, and all columns by using the asterisk (*) from a table.

SQL WHERESummary: In this tutorial, you will learn how to use SQL WHERE clause with SQL SELECT statement to filter the data you select from database tables. The SQL WHERE clause is used with other SQL statements such as SQL SELECT, SQL DELETE and SQL UPDATE statements to filter records in the database tables which satisfy specific row conditions. SQL provides you various operators to allow you to construct row conditions. Here are most common operators in SQL.


Operator Description = Equal > Greater than < Less than >= Greater than or equal ) in WHERE clause.SELECT lastname, firstname, title, country,date(hiredate) FROM employees WHERE hiredate > '1993-01-01'


lastname --------Peacock Buchanan Suyama King Callahan Dodsworth

firstname --------Margaret Steven Michael Robert Laura Anne

title -----------------------Sales Representative Sales Manager Sales Representative Sales Representative Inside Sales Coordinator Sales Representative

country ------USA UK UK UK USA UK

date(hiredate) -------------1993-05-03 1993-10-17 1993-10-17 1994-01-02 1994-03-05 1994-11-15

The logical operators such as OR and AND are used to combine multiple conditions in WHERE clause.

SQL WHERE with AND operatorMultiple conditions are joined by AND operator must be TRUE for entire condition evaluate to TRUE. If one of condition is false, the entire condition evaluates to FALSE as well. If one of condition is unknown or NULL the entire condition evaluates to UNKNOWN. (You will learn how to deal with NULL in the next tutorials later). TRUE FALSE UNKNOWN TRUE TRUE FALSE UNKNOWN FALSE FALSE FALSE FALSE UNKNOWN UNKNOWN FALSE UNKNOWN Suppose you want to find all employees who were hired after 1993 and lived in UK, you can use AND operator to combine both conditions like the following query:SELECT lastname, firstname, title, country, date(hiredate) FROM employees WHERE hiredate > '1993-01-01' AND country = 'USA' lastname firstname title country date(hiredate) -------- --------- ------------------------ ------- -------------Peacock Margaret Sales Representative USA 1993-05-03 Callahan Laura Inside Sales Coordinator USA 1994-03-05

SQL WHERE with OR operatorMultiple conditions joined by OR operator must be FALSE for entire condition is evaluated to FALSE. If one of condition is TRUE, the entire condition is evaluated to TRUE. If one of condition is UNKNOWN or NULL the entire condition is evaluated to UNKNOWN or NULL. TRUE FALSE UNKNOWN TRUE TRUE TRUE UNKNOWN FALSE TRUE FALSE UNKNOWN UNKNOWN TRUE UNKNOWN UNKNOWN For example you can find all employees who live in London or Seattle city, you can use OR operator in this case.SELECT firstname, lastname, city FROM employees WHERE city = 'London' OR city = 'Seattle' firstname lastname city --------- --------- -------


Nancy Steven Michael Robert Laura Anne

Davolio Buchanan Suyama King Callahan Dodsworth

Seattle London London London Seattle London

In this tutorial, youve learned how to filter the records in the result set in the SQL SELECT statement by using the SQL WHERE clause with conditions. Youve also learned how to use various common basic operators to construct a condition and use logical operators to combine condition together. In the next tutorial you will learn how to use other operator such as SQL BETWEEN and SQL IN to retrieve data in a range of values and in a set.

SQL AliasIn this tutorial, you will learn how to use different SQL alias including column alias and table alias with SQL SELECT statement. SQL Alias is used to organize the output and avoid ambiguous table error in SQL statements when multiple tables with the same column names are refered to. SQL supports two types of alias which are known as column alias and table alias.

SQL Column AliasBasically when you select data from a database table, the column headings of the output are the same as columns name. However, the column's name heading is too technical that does not bring you any information therefore you want to change it in the output. SQL provides you SQL alias to allow you to do so. You can assign a column alias in the column list of the SELECT statement by using AS keyword. Lets take a look at several examples of using SQL column alias.1 SELECT productName AS product, 2 unitPrice AS price 3 FROM products 4 WHERE unitPrice >50 product price ------------------------ -------Mishi Kobe Niku 97.0000 Carnarvon Tigers 62.5000 Sir Rodney's Marmalade 81.0000 Thringer Rostbratwurst 123.7900 Cte de Blaye 263.5000 Manjimup Dried Apples 53.0000 Raclette Courdavault 55.0000

In the SELECT statement above, we used two column aliases. The first column alias is product which represents for productname and the second alias is the price which represent for unitprice. In case the column name is lengthy and sometimes it is designed as an abbreviation, the column alias enables the output more meaningful and easy to read. 7

Be noted that the AS keyword is optional. You can omit the AS keyword in the column alias. If the column alias contain space but it must be enclosed in quotes. We can rewrite the above SELECT query as follows:1 SELECT productName product, 2 unitPrice "unit price" 3 FROM products 4 WHERE unitPrice > 50

SQL Table AliasSQL table alias is another name which you put after the table name of FROM clause of SELECT statement followed after table's name. SQL table alias is used when you refer a table multiple times in a SELECT statement with JOIN clause or when the table name is too long that yo