+ All Categories
Home > Engineering > Sql Tutorials

Sql Tutorials

Date post: 18-Jul-2015
Category:
Upload: priyabrat-kar
View: 97 times
Download: 5 times
Share this document with a friend
29
SQL Structured Query Language PRIYABRAT KAR
Transcript
Page 1: Sql Tutorials

SQL

Structured Query Language

PRIYABRAT KAR

Page 2: Sql Tutorials

• SQL is a standard language for accessing databases.

• SELECT * FROM Customers;

Page 3: Sql Tutorials

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

Page 4: Sql Tutorials

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

Page 5: Sql Tutorials

Semicolon after SQL Statements?

• Some database systems require a semicolon at the end of each SQL statement.

• Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server.

• In this tutorial, we will use semicolon at the end of each SQL statement.

Page 6: Sql Tutorials

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

Page 7: Sql Tutorials

The SQL SELECT Statement

The SELECT statement is used to select data from a database.

• The result is stored in a result table, called the result-set.

• SQL SELECT SyntaxSELECT column_name,column_nameFROM table_name;and

SELECT * FROM table_name;

Page 8: Sql Tutorials

The SQL WHERE Clause

• The WHERE clause is used to extract only those records that fulfill a specified criterion.

• SQL WHERE SyntaxSELECT column_name,column_nameFROM table_nameWHERE column_name operator value;

• The AND & OR operators are used to filter records based on more than one condition.

Page 9: Sql Tutorials

The SQL ORDER BY Keyword

• The ORDER BY keyword is used to sort the result-set by one or more columns.

• The ORDER BY keyword sorts the records in ascending order by default. To sort the records in a descending order, you can use the DESC keyword.

• SQL ORDER BY Syntax

SELECT column_name, column_nameFROM table_nameORDER BY column_name ASC|DESC, column_name ASC|DESC;

Page 10: Sql Tutorials

The SQL UPDATE Statement

• The UPDATE statement is used to update existing records in a table.

• SQL UPDATE Syntax

UPDATE table_nameSET column1=value1,column2=value2,...WHERE some_column=some_value;

Page 11: Sql Tutorials

The SQL DELETE Statement

• The DELETE statement is used to delete rows in a table.

• SQL DELETE Syntax

DELETE FROM table_nameWHERE some_column=some_value;

Page 12: Sql Tutorials

The SQL LIKE Operator

• The LIKE operator is used to search for a specified pattern in a column.

• SQL LIKE Syntax

SELECT column_name(s)FROM table_nameWHERE column_name LIKE pattern;

Page 13: Sql Tutorials

SQL Wildcard CharactersIn SQL, wildcard characters are used with the SQL LIKE operator.

• SQL wildcards are used to search for data within a table.

With SQL, the wildcards are:

Wildcard Description% A substitute for zero or more characters

_ A substitute for a single character

[charlist] Sets and ranges of characters to match

[!charlist] Matches only a character NOT specified within the brackets

Page 14: Sql Tutorials

SQL Joins

• SQL joins are used to combine rows from two or more tables.

Page 15: Sql Tutorials

Different SQL JOINs• Before we continue with examples, we will list the types the

different SQL JOINs you can use:

• INNER JOIN: Returns all rows when there is at least one match in BOTH tables

• LEFT JOIN: Return all rows from the left table, and the matched rows from the right table

• RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table

• FULL JOIN: Return all rows when there is a match in ONE of the tables

Page 16: Sql Tutorials

SQL INNER JOIN Keyword

• The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables.

• SQL INNER JOIN SyntaxSELECT column_name(s)FROM table1INNER JOIN table2ON table1.column_name=table2.column_name;or:

• SELECT column_name(s)FROM table1JOIN table2ON table1.column_name=table2.column_name;PS! INNER JOIN is the same as JOIN.

Page 17: Sql Tutorials
Page 18: Sql Tutorials

SQL LEFT JOIN Keyword

• The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.

• SQL LEFT JOIN SyntaxSELECT column_name(s)FROM table1LEFT JOIN table2ON table1.column_name=table2.column_name;or:

• SELECT column_name(s)FROM table1LEFT OUTER JOIN table2ON table1.column_name=table2.column_name;

Page 19: Sql Tutorials
Page 20: Sql Tutorials

SQL RIGHT JOIN Keyword

• The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match.

• SQL RIGHT JOIN SyntaxSELECT column_name(s)FROM table1RIGHT JOIN table2ON table1.column_name=table2.column_name;or:

• SELECT column_name(s)FROM table1RIGHT OUTER JOIN table2ON table1.column_name=table2.column_name;

Page 21: Sql Tutorials
Page 22: Sql Tutorials

SQL FULL OUTER JOIN Keyword

• The FULL OUTER JOIN keyword returns all rows from the left table (table1) and from the right table (table2).

• The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT joins.

• SQL FULL OUTER JOIN SyntaxSELECT column_name(s)FROM table1FULL OUTER JOIN table2ON table1.column_name=table2.column_name;

Page 23: Sql Tutorials
Page 24: Sql Tutorials

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.

• SQL CREATE TABLE SyntaxCREATE TABLE table_name(column_name1 data_type(size),column_name2 data_type(size),column_name3 data_type(size),....

);

Page 25: Sql Tutorials

SQL ConstraintsSQL constraints are used to specify rules for the data in a table.

• If there is any violation between the constraint and the data action, the action is aborted by the constraint.

• Constraints can be specified when the table is created (inside the CREATE TABLE statement) or after the table is created (inside the ALTER TABLE statement).

• SQL CREATE TABLE + CONSTRAINT SyntaxCREATE TABLE table_name(column_name1 data_type(size) constraint_name,column_name2 data_type(size) constraint_name,column_name3 data_type(size) constraint_name,....);

Page 26: Sql Tutorials

In SQL, we have the following constraints:

• NOT NULL - Indicates that a column cannot store NULL value

• UNIQUE - Ensures that each row for a column must have a unique value

• PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Ensures that a column (or combination of two or more columns) have an unique identity which helps to find a particular record in a table more easily and quickly

• FOREIGN KEY - Ensure the referential integrity of the data in one table to match values in another table

• CHECK - Ensures that the value in a column meets a specific condition

• DEFAULT - Specifies a default value when specified none for this column

Page 27: Sql Tutorials

SQL Aggregate Functions

• SQL aggregate functions return a single value, calculated from values in a column.

• Useful aggregate functions:

AVG() - Returns the average valueCOUNT() - Returns the number of rowsFIRST() - Returns the first valueLAST() - Returns the last valueMAX() - Returns the largest valueMIN() - Returns the smallest valueSUM() - Returns the sum

Page 28: Sql Tutorials

SQL Scalar functions

• SQL scalar functions return a single value, based on the input value.

• Useful scalar functions:

UCASE() - Converts a field to upper caseLCASE() - Converts a field to lower caseMID() - Extract characters from a text fieldLEN() - Returns the length of a text fieldROUND() - Rounds a numeric field to the number of

decimals specifiedNOW() - Returns the current system date and timeFORMAT() - Formats how a field is to be displayed

Page 29: Sql Tutorials

Thank You


Recommended