Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction To Structured Query Language (SQL)

Similar presentations


Presentation on theme: "Introduction To Structured Query Language (SQL)"— Presentation transcript:

1 Introduction To Structured Query Language (SQL)

2 SQL SQL is a standard language for accessing databases.
Our SQL tutorial will teach you how to use SQL to access and manipulate data in: MS Access SQL Server database systems.

3 What is SQL? SQL stands for Structured Query Language
SQL lets you access and manipulate databases SQL is an ANSI (American National Standards Institute) standard

4 What Can SQL do? SQL can execute queries against a database
SQL can retrieve data from a database SQL can insert records in a database SQL can update records in a database SQL can delete records from a database SQL can create new databases SQL can create new tables in a database SQL can create stored procedures in a database SQL can create views in a database SQL can set permissions on tables, procedures, and views

5 Some of The Most Important SQL Commands
SELECT - extracts data from a database UPDATE - updates data in a database DELETE - deletes data from a database INSERT INTO - inserts new data into a database CREATE DATABASE - creates a new database ALTER DATABASE - modifies a database CREATE TABLE - creates a new table ALTER TABLE - modifies a table DROP TABLE - deletes a table CREATE INDEX - creates an index (search key) DROP INDEX - deletes an index

6 Student

7

8 SELECT Statements In this tutorial we will use the well-known sample database (included in MS Access and MS SQL Server). The following SQL statement selects all the records in the table: Statement: SELECT * FROM Table; E.g.: SELECT * FROM Student;

9 SELECT Statements SELECT column_name,column_name FROM table_name;
Select id, Firstname, age From Student;

10 SQL SELECT DISTINCT Statement
The SELECT DISTINCT statement is used to return only distinct (different) values. SELECT DISTINCT column_name,column_name FROM table_name;

11 SQL WHERE Clause The WHERE clause is used to filter records.
SELECT * FROM Student WHERE Field_Name ="Text Value"; SELECT * FROM Student WHERE Field_Name =Numeric Value;

12 Operators in The WHERE Clause
Description = Equal <> Not equal. Note: In some versions of SQL this operator may be written as != > Greater than < Less than >= Greater than or equal <= Less than or equal BETWEEN Between an inclusive range LIKE Search for a pattern IN To specify multiple possible values for a column

13 AND & OR Operators The AND & OR operators are used to filter records based on more than one condition. SELECT * FROM Student WHERE City =Nizwa" AND Age < 30; SELECT * FROM Student WHERE City =Nizwa" OR Age < 30; SELECT * FROM Student WHERE City=Nizwa" AND (lastname= "said" OR firstname="ali");

14 Operators in The WHERE Clause
The BETWEEN operator selects values within a range. The values can be numbers, text, or dates. SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2; Not Between used to NOT Include SELECT Firstname, Lastname, city, age FROM student WHERE city NOT BETWEEN "Muscat" AND "Izki";

15 IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SELECT column_name(s) FROM table_name WHERE column_name IN (value1,value2,...);

16 LIKE Operator The LIKE operator is used to search for a specified pattern in a column. SELECT * FROM Student WHERE City LIKE "s*";

17 ORDER BY Keyword The ORDER BY keyword is used to sort the result-set.
SELECT * FROM student ORDER BY city; SELECT * FROM student ORDER BY city ASC /DESC; SELECT * FROM student ORDER BY city, firstname; SELECT * FROM student ORDER BY city ASC, lastname DESC;

18 INSERT INTO Statement The INSERT INTO statement is used to insert new records in a table. INSERT INTO Student(FirstName, LastName, City, age, Mob) VALUES (‘Nour',Elshaiekh',Halfa',42, );

19 UPDATE Statement The UPDATE statement is used to update records in a table. UPDATE student SET FirstName = ‘ALSaadi' WHERE d =4; UPDATE Student SET LastName= “Mussab”, City= “Suhar” WHERE FirstName= “Ali”;

20 Update table with data from another table
UPDATE ab SET current_category = (SELECT category_id FROM products WHERE products.product_id = summary_data.product_id) WHERE EXISTS (SELECT category_id FROM products WHERE products.product_id = summary_data.product_id);

21 Delete Statement The DELETE statement is used to delete records in a table. DELETE FROM Student WHERE FirstName="Ali" AND LastName="Said" ;

22 SELECT TOP Clause The SELECT TOP clause is used to specify the number of records to return. SELECT TOP 2 * FROM Student;

23 SELECT TOP Clause SQL statement selects the first ?% of the records from the table: SELECT TOP 20 PERCENT * FROM Student;

24 SQL JOIN An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them. SELECT abs.name, Student.FirstName, ab.age FROM student INNER JOIN ab ON ab.Name=student.FirstName;

25 SQL Aliases SQL aliases are used to give a database table, or a column in a table, a temporary name. Basically aliases are created to make column names more readable. SELECT FirstName AS CEMIS FROM Student;

26 The SQL UNION Operator The UNION operator is used to combine the result-set of two or more SELECT statements SELECT FirstName FROM Student UNION SELECT FirstName FROM ab; SELECT City FROM Student SELECT FirstName From ab ORDER BY City;

27 SQL SELECT INTO Statement
With SQL, you can copy information from one table into another. The SELECT INTO statement copies data from one table and inserts it into a new table. We can copy all columns into the new table: SELECT * INTO newtable [IN externaldb] FROM table1;

28 SQL SELECT INTO Statement
Or we can copy only the columns we want into the new table: SELECT column_name(s) INTO newtable [IN externaldb] FROM table1;

29 SQL SELECT INTO Statement
Copy only the German customers into the new table: SELECT * INTO qq FROM Student WHERE City='Muscat';

30 SQL SELECT INTO Statement
INSERT INTO table2 (column_name(s)) SELECT column_name(s) FROM table1;

31 The SQL CREATE TABLE Statement
The CREATE TABLE statement is used to create a table in a database. Tables are organized into rows and columns; and each table must have a name. REATE TABLE table_name ( column_name1 data_type(size), column_name2 data_type(size), column_name3 data_type(size), .... );

32 The SQL CREATE TABLE Statement
CREATE TABLE Staff ( StaffID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) );

33 CREATE TABLE StaffNotNull ( S_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) )


Download ppt "Introduction To Structured Query Language (SQL)"

Similar presentations


Ads by Google