SQL- STRUCTURED QUERY
LANGUAGE
PART - 1
INTRODUCTION
• It is a computer programming language that is used for storage,
retrieval and manipulation of data that is stored in relational database.
This is a standard computer programming language used for RDMS
(Relational Database Management Systems).
• IBM’s Ted Cod a.k.a Father of Relational databases gave the concept of
relational model for database in 1970. It was 4 years later SQL appeared
in 1974. This was just an idea, which got conceptualized in the form of
Systems/R in 1978 and was released by IBM. The ANSI standards and
first prototype of relational databases was released in 1986, which is
popularly knows as Oracle
ADVANTAGE
• Used for accessing data in RDBMS.
• Used for describing data.
• Definition of data and its manipulation.
• Can be used with other programming language by embedding SQL
modules into other languages code, pre-compilers and libraries.
• Possible to create and drop data base using this programming
language.
• Setting permission on views, table and procedures.
• Can be used for creating views, procedures and functions.
COMMANDS
Commands in SQL are categorized into three category namely
• DDL – Data definition language
• DML – Data Manipulation language
• DCL – Data Control language
DATA DEFINITION LANGUAGE (DDL)
Commands that are classified under DDL category are as follows:
• CREATE – Used for creating an object, table/view.
• ALTER – Used for modifying an existing database object.
• DROP – Object, table an views created using CREATE can be
deleted/removed.
DATA MANIPULATION LANGUAGE (DML)
Commands that are classified under DML are as follows:
• SELECT – Used for retrieving a set of records from one/more than one
tables.
• DELETE – Used for deleting records.
• UPDATE – Used for modifying / updating records.
• INSERT – Used for inserting records.
DATA CONTROL LANGUAGE
Commands that have been classified under DCL are:
GRANT – Users can be granted permission / privileges using this command
REVOKE – Privileges to the user can be taken back using this command.
CONSTRAINTS
Rules are enforced on the columns of the table that contain data specific
for the field for all the record in the table. These rules are referred to as
constraints, which are generally used to ensure that field only gets a
particular type of value. For instance if there is a field called “Age” in the
table, then this field can only take numeric value.
Constraints set up for the table apply to all the data stored in the table.
Some of the common constraints are:
NOT NULL:
This constraints ensure that the field value is never set to NULL
DEFAULT:
Typically used to fill in a default value for any field left blank.
UNIQUE:
If the constraints is set on a column, then all value set for this field will have to be unique
PRIMARY KEY:
When this constraint is set for a column, it indicates that the field is the primary key/the
field value is a unique identifier for every record existent in the table. One Primary key
per table.
FOREIGN KEY:
When this constraint is set for a column, it indicates that the field is the
foreign key/the field value is a unique identifier for every record existent in
another table.
CHECK:
Checks if the values for a field satisfy pre-defined conditions
INDEX:
Can be used as an index (column) which shall be used for faster data
retrieval from the table.
DATA INTEGRITY
It is the measure of the accuracy and consistency of data stored in the
data-base.
ENTITY INTEGRITY:
Refers to the accuracy and consistency of each entity in the database,
which is each record. Therefore, every record must be unique and no
duplicates of any of the records must be there.
DOMAIN INTEGRITY:
Ensures that every field gets valid entries by imposing restrictions on the
type, format and range of values that a field can hold
REFERENTIAL INTEGRITY
Records/rows that are being used by other able in the database cannot be
deleted.
USER-DEFINED INTEGRITY
Any rules that the user wishes to impose on columns and that are not
covered in the other data integrity categories form a part of user defined
integrity category
SYNTAX
SYNTAX
• Every SQL statement should start with a keyword. Some example of this
include SELECT, CREATE, INSERT, DELETE & ALTER.
• Every SQL statement terminates with a semicolon (;)
• SQL is case insensitive
SYNTAX RULES
col Column
t_name Table Name
col_name Column Name
V Value
i_name Index Name
db_name Database Name
cond Conditions
Asterisk (*) means all
SHOW DATABASE:
To see the complete list of commands
SHOW TABLES:
To view a list of tables for the currently selected database
SHOW COLUMNS:
Displays information about the columns in a given table
Eg. SHOW COLUMNS FROM customers;
SELECT:
Is used to select data from a database. The result is stored in a result table, which is called
result – set.
Eg. SELECT first name FROM customers;
MULTIPLE QUERIES:
SELECT first name FROM customer;
SELECT city FROM customers;
SELECTING MULTIPLE QUERIES:
SELECT first name, last name, city FROM customers;
SELECTING ALL COLUMNS:
To retrieve all of the information contained in your table, place an asterisk
(*) sign after the SELECT command.
Eg. SELECT * FROM customers;
DISTINCT:
In situations in which you have multiple duplicates records in a table, it
might make more sense to return only unique records, instead of fetching
duplicates.
Eg. SELECT DISTINCT column_name1, column_name2 FROM table_name.
LIMIT:
By default, all results that satisfy the conditions specified in the SQL
statement are returned. However, sometimes we need to retrieve just a
subset of records. In MySQL, this is accomplished by using the LIMIT
keyword.
SELECT column list FROM table_name LIMIT [Number of records]
Also allows to pick up a set of records from a particular offset.
Eg. We pick up 4 records, starting from the third position
SELECT ID, first name, last name, city FROM customers LIMIT 3,4;
FULLY QUALIFIED NAME:
In SQL, you can provide the table name prior to the column name by
separating them with a dot.
Eg. SELECT city FROM customer;
SELECT customer.city from customer;
ORDER BY:
It is used with SELECT to sort the return data.
SELECT * FROM customers ORDER BY first name;
For multiple columns, the ORDER BY command starts ordering in the same
sequence as the columns It will order by the first column listed, then by the
second and so on.
SELECT * FROM customers ORDER BY last name, age;
WHERE:
Where clause is used to extract only those records that fulfill a specified
condition.
SELECT column_list FROM table_name WHERE condition
COMPARISON OPERATORS
Comparison and Logical Operators are used in the WHERE clause to filter
the data to be used.
OPERATOR DESCRIPTON
= Equal
!= Not Equal
> Greater than
< Less than
>= Greater than/Equal
<= Less than/Equal
BETWEEN Between an exclusive range
Examples of Comparison Operators:
SELECT * FROM customer WHERE id !=5;
SELECT * FROM customer WHERE id BETWEEN 3 AND 7
SELECT id, first name, last name, city FROM customer WHERE city = “New
York”;
FYI incase the text contains apostrophe, use ‘Can’‘t’.
THANK YOU

SQL: Structured Query Language

  • 1.
  • 2.
    INTRODUCTION • It isa computer programming language that is used for storage, retrieval and manipulation of data that is stored in relational database. This is a standard computer programming language used for RDMS (Relational Database Management Systems). • IBM’s Ted Cod a.k.a Father of Relational databases gave the concept of relational model for database in 1970. It was 4 years later SQL appeared in 1974. This was just an idea, which got conceptualized in the form of Systems/R in 1978 and was released by IBM. The ANSI standards and first prototype of relational databases was released in 1986, which is popularly knows as Oracle
  • 3.
    ADVANTAGE • Used foraccessing data in RDBMS. • Used for describing data. • Definition of data and its manipulation. • Can be used with other programming language by embedding SQL modules into other languages code, pre-compilers and libraries. • Possible to create and drop data base using this programming language. • Setting permission on views, table and procedures. • Can be used for creating views, procedures and functions.
  • 4.
    COMMANDS Commands in SQLare categorized into three category namely • DDL – Data definition language • DML – Data Manipulation language • DCL – Data Control language
  • 5.
    DATA DEFINITION LANGUAGE(DDL) Commands that are classified under DDL category are as follows: • CREATE – Used for creating an object, table/view. • ALTER – Used for modifying an existing database object. • DROP – Object, table an views created using CREATE can be deleted/removed.
  • 6.
    DATA MANIPULATION LANGUAGE(DML) Commands that are classified under DML are as follows: • SELECT – Used for retrieving a set of records from one/more than one tables. • DELETE – Used for deleting records. • UPDATE – Used for modifying / updating records. • INSERT – Used for inserting records.
  • 7.
    DATA CONTROL LANGUAGE Commandsthat have been classified under DCL are: GRANT – Users can be granted permission / privileges using this command REVOKE – Privileges to the user can be taken back using this command.
  • 8.
    CONSTRAINTS Rules are enforcedon the columns of the table that contain data specific for the field for all the record in the table. These rules are referred to as constraints, which are generally used to ensure that field only gets a particular type of value. For instance if there is a field called “Age” in the table, then this field can only take numeric value. Constraints set up for the table apply to all the data stored in the table.
  • 9.
    Some of thecommon constraints are: NOT NULL: This constraints ensure that the field value is never set to NULL DEFAULT: Typically used to fill in a default value for any field left blank. UNIQUE: If the constraints is set on a column, then all value set for this field will have to be unique PRIMARY KEY: When this constraint is set for a column, it indicates that the field is the primary key/the field value is a unique identifier for every record existent in the table. One Primary key per table.
  • 10.
    FOREIGN KEY: When thisconstraint is set for a column, it indicates that the field is the foreign key/the field value is a unique identifier for every record existent in another table. CHECK: Checks if the values for a field satisfy pre-defined conditions INDEX: Can be used as an index (column) which shall be used for faster data retrieval from the table.
  • 11.
    DATA INTEGRITY It isthe measure of the accuracy and consistency of data stored in the data-base. ENTITY INTEGRITY: Refers to the accuracy and consistency of each entity in the database, which is each record. Therefore, every record must be unique and no duplicates of any of the records must be there. DOMAIN INTEGRITY: Ensures that every field gets valid entries by imposing restrictions on the type, format and range of values that a field can hold
  • 12.
    REFERENTIAL INTEGRITY Records/rows thatare being used by other able in the database cannot be deleted. USER-DEFINED INTEGRITY Any rules that the user wishes to impose on columns and that are not covered in the other data integrity categories form a part of user defined integrity category
  • 13.
  • 14.
    SYNTAX • Every SQLstatement should start with a keyword. Some example of this include SELECT, CREATE, INSERT, DELETE & ALTER. • Every SQL statement terminates with a semicolon (;) • SQL is case insensitive
  • 15.
    SYNTAX RULES col Column t_nameTable Name col_name Column Name V Value i_name Index Name db_name Database Name cond Conditions Asterisk (*) means all
  • 16.
    SHOW DATABASE: To seethe complete list of commands SHOW TABLES: To view a list of tables for the currently selected database SHOW COLUMNS: Displays information about the columns in a given table Eg. SHOW COLUMNS FROM customers; SELECT: Is used to select data from a database. The result is stored in a result table, which is called result – set. Eg. SELECT first name FROM customers;
  • 17.
    MULTIPLE QUERIES: SELECT firstname FROM customer; SELECT city FROM customers; SELECTING MULTIPLE QUERIES: SELECT first name, last name, city FROM customers; SELECTING ALL COLUMNS: To retrieve all of the information contained in your table, place an asterisk (*) sign after the SELECT command. Eg. SELECT * FROM customers;
  • 18.
    DISTINCT: In situations inwhich you have multiple duplicates records in a table, it might make more sense to return only unique records, instead of fetching duplicates. Eg. SELECT DISTINCT column_name1, column_name2 FROM table_name. LIMIT: By default, all results that satisfy the conditions specified in the SQL statement are returned. However, sometimes we need to retrieve just a subset of records. In MySQL, this is accomplished by using the LIMIT keyword. SELECT column list FROM table_name LIMIT [Number of records] Also allows to pick up a set of records from a particular offset. Eg. We pick up 4 records, starting from the third position SELECT ID, first name, last name, city FROM customers LIMIT 3,4;
  • 19.
    FULLY QUALIFIED NAME: InSQL, you can provide the table name prior to the column name by separating them with a dot. Eg. SELECT city FROM customer; SELECT customer.city from customer; ORDER BY: It is used with SELECT to sort the return data. SELECT * FROM customers ORDER BY first name; For multiple columns, the ORDER BY command starts ordering in the same sequence as the columns It will order by the first column listed, then by the second and so on. SELECT * FROM customers ORDER BY last name, age;
  • 20.
    WHERE: Where clause isused to extract only those records that fulfill a specified condition. SELECT column_list FROM table_name WHERE condition
  • 21.
    COMPARISON OPERATORS Comparison andLogical Operators are used in the WHERE clause to filter the data to be used. OPERATOR DESCRIPTON = Equal != Not Equal > Greater than < Less than >= Greater than/Equal <= Less than/Equal BETWEEN Between an exclusive range
  • 22.
    Examples of ComparisonOperators: SELECT * FROM customer WHERE id !=5; SELECT * FROM customer WHERE id BETWEEN 3 AND 7 SELECT id, first name, last name, city FROM customer WHERE city = “New York”; FYI incase the text contains apostrophe, use ‘Can’‘t’.
  • 23.