Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL SQL Ayshah I. Almugahwi 200800180 Maryam J. Alkhalifa 200801068.

Similar presentations


Presentation on theme: "SQL SQL Ayshah I. Almugahwi 200800180 Maryam J. Alkhalifa 200801068."— Presentation transcript:

1 SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa

2 Introduction to sql What is SQL? What can SQL do?
- SQL stands for Structured Query Language - SQL is a special-purpose language used to define, access, and manipulate data - It has special rules of grammar (syntax and semantics) What can SQL do? - SQL can execute queries SQL can retrieve data from a database - SQL can insert, update, and delete records in a database - SQL can create new tables in a database

3 Sql Commands The SQL language includes two distinct sets of commands:
Data Definition Language DDL Data Manipulation Language DML includes numerous commands for handling specific tasks such as: Create indexes Views Constraints Drop Alter comprised of just five statements : INSERT UPDATE DELETE MERGE SELECT

4 Select Statement Syntax: Example:
- used to query the database and retrieve selected data that match the criteria that you specify. Syntax: SELECT [ALL | DISTINCT] column1[,column2] FROM table1[,table2] [WHERE "conditions"] [GROUP BY "column-list"] [HAVING "conditions] [ORDER BY "column-list" [ASC | DESC] ]; Example: SELECT name, age, salary FROM employee WHERE age > 50; Remember to put a semicolon at the end of your SQL statements. it indicates that your SQL statement is complete and is ready to be executed

5 Select Statement (cont.)
Comparison Operator: Sign Name > Greater than < Less than >= Greater than or equal to <= Less than or equal to <> Or != Not equal to LIKE String comparison test

6 Select Statement (cont.)
Example for LIKE: SELECT name, title, dept FROM employee WHERE title LIKE 'Pro%'; LIKE in Access: LIKE [Enter the first char to search by: ] & "*" -or- LIKE "*" & [Enter any char to search by: ] & "*“ Example: SELECT F_name, L_name, age, salary FROM employee WHERE F_name LIKE [A] & "*" -or- WHERE F_name LIKE "*" & [A] & "*“;

7 Functions Example 1: Example 2: Function Description
MIN returns the smallest value in a given column MAX returns the largest value in a given column AVG returns the average value of a given column SUM returns the sum of the numeric values in a given column COUNT returns the total number of values in a given column Example 1: Example 2: SELECT AVG(salary) FROM employee; SELECT AVG(salary) FROM employee WHERE title = 'Programmer';

8 Group By statement Syntax: Example:
- Gather all of the rows together that contain data in the specified column(s) and will allow aggregate functions to be performed on the one or more columns. Syntax: SELECT column1, SUM(column2) FROM "list-of-tables" GROUP BY "column-list"; Example: SELECT max(salary), dept FROM employee GROUP BY dept;

9 Having statement Syntax: Example:
- Allows you to specify conditions on the rows for each group - in other words, which rows should be selected will be based on the conditions you specify. The HAVING clause should follow the GROUP BY clause if you are going to use it. Syntax: SELECT column1, SUM(column2) FROM "list-of-tables" GROUP BY "column-list" HAVING "condition"; Example: SELECT dept, avg(salary) FROM employee GROUP BY dept HAVING avg(salary) > 20000;

10 Order by statement Syntax: Example:
- ORDER BY is an optional clause which will allow you to display the results of your query in a sorted order (either ascending order or descending order) based on the columns that you specify to order by. Syntax: SELECT column1 FROM "list-of-tables" ORDER BY "column-list" [ASC | DESC]; Example: SELECT employee_id, dept, name, age, salary FROM employee_info WHERE dept = 'Sales‘ ORDER BY salary DESC;

11 Combining conditions And & or operator s Syntax: Example:
- The AND and OR operator can be used to join two or more conditions in the WHERE clause. Syntax: SELECT column1, SUM(column2) FROM "list-of-tables" WHERE "condition1" AND "condition2"; Example: SELECT employeeid, firstname, lastname, title, salary FROM employee_info WHERE salary >= AND title = 'Programmer'; you can use parenthesis around your conditional expressions to make it easier to read but they are not required

12 Between operator Syntax: Example:
- The BETWEEN conditional operator is used to test to see whether or not a value is "between" two other values. Syntax: SELECT col1, col2 FROM table WHERE value BETWEEN value AND value; Example: SELECT employeeid, age, lastname, salary FROM employee_info WHERE age BETWEEN 30 AND 40;

13 Insert Into Statement Syntax: Example:
- The INSERT INTO statement is used to insert a new row in a table Syntax: INSERT INTO table_name VALUES (value1, value2, value3,...) Example: INSERT INTO Persons VALUES (4,'Nilsen', 'Johan', 'Bakken 2', 'Stavanger')

14 Up date statement Syntax: Example:
- The UPDATE statement is used to update existing records in a table. Syntax: UPDATE table_name SET column1=value, column2=value2,... WHERE some_column=some_value Example: UPDATE Persons SET Address='Nissestien 67', City='Sandnes' WHERE LastName='Tjessem' AND FirstName='Jakob‘ - Be careful when updating records. If we omitted the WHERE clause in the previous example all the values of address and city column will be the same.

15 Table joins Syntax: Example:
- The "Join" makes relational database systems "relational". Syntax: SELECT "list-of-columns" FROM [table1] INNER JOIN [table2] ON [table1].Primary KEY=[table2].Foreign Key WHERE "search-condition(s)“ Example: SELECT [customer_info] .Firstname, [customer_info] . Lastname, [purchases].item FROM [customer_info] INNER JOIN [purchases] ON [customer_info] .CustomerID= [purchases] . CustomerID WHERE “[customer_info.]. CustomerID = 12345”;

16 References: SQL Couse.com, Interactive Online SQL Training ( W3 School, Introduction SQL (

17 Thank You For Listening


Download ppt "SQL SQL Ayshah I. Almugahwi 200800180 Maryam J. Alkhalifa 200801068."

Similar presentations


Ads by Google