0% found this document useful (0 votes)
126 views

SQL v1 PDF

SQL is a programming language used to manage data in relational database systems. It allows users to define, manipulate, and control data as well as manage the database structure. Common SQL commands include SELECT to query data, INSERT and UPDATE to manipulate data, CREATE and ALTER to define the database structure, and DROP to delete tables. SQL also supports constraints to define rules for data within tables.

Uploaded by

Bhanupriya M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views

SQL v1 PDF

SQL is a programming language used to manage data in relational database systems. It allows users to define, manipulate, and control data as well as manage the database structure. Common SQL commands include SELECT to query data, INSERT and UPDATE to manipulate data, CREATE and ALTER to define the database structure, and DROP to delete tables. SQL also supports constraints to define rules for data within tables.

Uploaded by

Bhanupriya M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

SELECT FROM

(COLUMN_NAME) (TABLE_NAME)

WHERE
(SEARCH CONDITION)

ORDER BY
(COLUMN_NAME)

GROUP BY HAVING
(SEARCH CONDITION)

JOIN USING
(TABLE_NAME) (COLUMN_NAME)
What is SQL?
SQL = Structured Query Language

a programming language specifically designed for working with Data bases


lets you create - access - share - manipulate databases
What is SQL?
SQL = Structured Query Language

a programming language specifically designed for working with Data bases


lets you create - access - share - manipulate databases

Database is a systematic collection of data stored in a format that can easily


be accessed.

A Database most often contains one or more tables. Each table is identified by a
name – “Customers”, “Orders”.

Table contains records(rows) with data.

To manage these databases we use applications called DBMS (DataBase Management


Systems)
What is SQL?
SQL = Structured Query Language

In SQL we have two main databases:

Relational Non Relational


What is Relational DataBases?

CUSTOMER_NAME ORDERS_NAME

RDBMS is the basis for SQL.

The data in RDBMS is stored PRODUCTS_NAME


in database objects called EMPLOYEE_ID
tables.

A table is a collections of
related data entries and it
consists of columns & rows STORE_NAME
COUNTRY_NAME
DDL, DQL, DML, DCL and TCL Commands
These SQL commands are mainly categorized into four categories as:

DDL – Data Definition Language

DQl – Data Query Language

DML – Data Manipulation Language

DCL – Data Control Language

TCL – Transaction Control Language


DDL(Data Definition Language) : DDL is a set of SQL commands that can be used
to define the db schema. It simply deals with descriptions of the db schema
and is used to create and modify the structure of db objects in the db.

DQL (Data Query Language) : DQL statements are used for performing queries on
the data within schema objects. The purpose of DQL Command is to get some
schema relation based on the query passed to it.

DML(Data Manipulation Language) : The SQL commands that deals with the
manipulation of data present in the database belong to DML or Data
Manipulation Language and this includes most of the SQL statements.

DCL(Data Control Language) : DCL includes commands such as GRANT and REVOKE
which mainly deals with the rights, permissions and other controls of the
database system.

TCL(Transaction Control Language) : TCL commands deals with the transaction


within the database.
DDL DCL DQL DML
Create Grant Select Insert

Alter Revoke Update

Drop Delete

Rename Merge

Truncate

Comment
Create
CREATE DATABASE is the SQL command for creating a database.

CREATE DATABASE movies;

Tables can be created using CREATE TABLE statement and it has the
following syntax.

CREATE TABLE [IF NOT EXISTS] `TableName` (`fieldname` dataType


[optional parameters])
Select
The SELECT statement helps retrieving data from the database
tables. It's part of the data query language that is responsible
for query the data from the database.

SELECT * FROM `table_name`;


Insert Into
The main goal of database systems is to store data in the tables.
The data is usually supplied by application programs that run on
top of the database. Towards that end, SQL has the INSERT command
that is used to store data into a table. The INSERT command
creates a new row in the table to store data.

INSERT INTO `table_name`(column_1,column_2,...) VALUES (value_1,value_2,...);


Update
The Update command is used to modify rows(existing data) in a
table. The update command can be used to update a single field or
multiple fields at the same time. It can also be used to update a
table with values from another table .

UPDATE `table_name` SET `column_name` = `new_value' [WHERE condition];

The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!
Alter Table
The Alter Table statement is used to add, delete or rename
columns in an existing table.

ALTER TABLE `table_name` ADD `column_name` datatype;


Drop
DROP is used to delete a whole database or just a table. The DROP statement
destroys the objects like an existing database, table, index, or view.
A DROP statement in SQL removes a component from a relational database
management system.

No DML triggers will be fired.


The operation cannot be rolled back.
DROP and TRUNCATE are DDL commands, whereas DELETE is a DML command.

DROP TABLE table_name;

DROP DATABASE database_name;


Truncate
Truncate preserves the structure of the table for future use, unlike drop table where
the table is deleted with its full structure.

• TRUNCATE is a DDL command


• We cannot use WHERE clause with TRUNCATE.
• TRUNCATE removes all rows from a table.
• TRUNCATE TABLE removes the data by deallocating the data pages used to
store the table data and records only the page deallocations in the
transaction log.
• To use Truncate on a table you need at least ALTER permission on the table.
• Truncate uses less transaction space than the Delete statement.
• Truncate cannot be used with indexed views.
• TRUNCATE is faster than DELETE.

TRUNCATE TABLE student_details;


Delete
The DELETE command is used to delete rows that are no longer required from the
database tables. It deletes the whole row from the table. Delete command comes
in handy to delete temporary or obsolete data from your database.

The DELETE command can delete more than one row from a table in a single query.
This proves to be advantages when removing large numbers of rows from a
database table

DELETE is a DML command.


We can use where clause with DELETE to filter & delete specific records.
It maintain the log, so it slower than TRUNCATE.
Delete uses the more transaction space than Truncate statement.
Delete can be used with indexed views.

DELETE FROM `table_name` [WHERE condition];


Be careful when deleting records. You cannot undo this statement!
Difference between Drop, Delet &Truncate
Drop Truncate Delete
DELETE is a DML command
DROP is a DDL command
Where clause with DELETE
DROP command removes a TRUNCATE is a DDL command to filter & delete
table from the DB. specific records
TRUNCATE removes all rows
Tables' rows, indexes and from a table DELETE statement removes
privileges will also be rows one at a time and
removed We cannot use WHERE records an entry in the
clause with TRUNCATE transaction log for each
Operation cannot be deleted row
rolled back.
Grant , Revoke
Grant gives(or grants) certain permissions to users

GRANT type_of_permission ON db_name.table_name TO ‘username’@’domain’

Revoke takes off certain permissions from users

REVOKE type_of_permission ON db_name.table_name FROM ‘username’@’domain’


Constraints
Constraints are used to limit the type of data that can go into a table.

Constraints can be specified when a table is created (with the CREATE TABLE
statement) or after the table is created (with the ALTER TABLE statement)

PRIMARY KEY
UNIQUE
FOREIGN KEY
NOT NULL
DEFAULT
CHECK
Constraints
PRIMARY KEY: A primary key is a field which can uniquely identify each row in a
table. And this constraint is used to specify a field in a table as primary key.
Each table should have PK & only one PK

UNIQUE: The UNIQUE constraint uniquely identifies each record in a database


table. That is, the values in any row of a column must not be repeated.

FOREIGN KEY: A Foreign key is a field which can uniquely identify each row in a
another table. And this constraint is used to specify a field as Foreign key.

NOT NULL: This constraint tells that we cannot store a null value in a column.
That is, if a column is specified as NOT NULL then we will not be able to store
null in this particular column any more.

DEFAULT: This constraint specifies a default value for the column when no value
is specified by the user.

CHECK: This constraint is used to limit the value range that can be placed in a
column.
Datatypes
Numeric String Date & Time

INT CHAR(M) DATE

TINYINT VARCHAR(M) DATETIME

SMALLINT BLOB or TEXT TIMESTAMP

MEDIUMINT ENUM TIME

BIGINT

FLOAT(M,D)
https://dev.mysql.com/doc/refman/8.0/en/data-types.html
Order of execution of a Query
SELECT DISTINCT column, AGG_FUNC(column_or_expression)

FROM mytable

JOIN another_table

ON mytable.column = another_table.column

WHERE constraint_expression

GROUP BY column

HAVING constraint_expression

ORDER BY column ASC/DESC

LIMIT count;
Thorough SQL
Retrieving , Restricting and sorting data(DQL)
Basic select statement with arithmetic operators

Using Literal Character Strings

Alternative Quote Operator

Using various clause with select statement like


between,in,like,and,or, not etc

Using order by clause


Create and Manage table Including Constraints(DDL)
Database Objects Creating Tables
Data Types Including Constraints
NOT NULL Constraint PRIMARY KEY Constraint
FOREIGN KEY Constraint CHECK Constraint
ALTER TABLE Statement Dropping a Table
Data Manipulation(DML) in depth
INSERT Statement, Inserting Rows with Null Values, Inserting Specific Date
Values

UPDATE Statement

DELETE Statement

TRUNCATE Statement
Left Join Right Join

Inner Join Outer Join


Joins
Inner Join Outer Join
Joins
Inner Join Outer Join

Left Outer Join Right Outer Join

Full Outer Join


Joins

Table A Table B
Joins

Table A Table B

On : Table A Customer = Table B Employee


Inner Join

Table A Table B

On : Table A Customer = Table B Employee


Left Outer Join

Table A Table B

On : Table A Customer = Table B Employee


Right Outer Join

Table A Table B

On : Table A Customer = Table B Employee


Full Outer Join

Table A Table B

On : Table A Customer = Table B Employee


Joins - Duplicates

Table A Table B
Joins - Duplicates
Displaying data from multiple tables (JOINS)
Obtaining data form multiple tables
Types of joins = Inner Join & Outer Join
Left outer joins, Right outer joins, Full outer joins

Creating Joins with the ON Clause


Retrieving Records with the ON Clause
Self-Joins Using the ON Clause

Creating Joins with the USING Clause


Retrieving Records with the USING Clause
Cartesian products

Creating Natural Joins


Retrieving Records with Natural Joins
Functions
Character functions
Number functions
Date functions
General functions
Using Case statements
Sub-Queries
A Subquery or Inner query is a query within another SQL query.

A subquery is used to return data that will be used in the main query as a
condition to further restrict the data to be retrieved.

Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements
along with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
Sub-Queries
There are a few rules that subqueries must follow −

Subqueries must be enclosed within parentheses.

A subquery can have only one column in the SELECT clause, unless multiple columns
are in the main query for the subquery to compare its selected columns.

An ORDER BY command cannot be used in a subquery, although the main query can use
an ORDER BY. The GROUP BY command can be used to perform the same function as the
ORDER BY in a subquery.

Subqueries that return more than one row can only be used with multiple value
operators such as the IN operator.
Sub-Queries
The SELECT list cannot include any references to values that evaluate to a
BLOB, ARRAY, CLOB, or NCLOB.

A subquery cannot be immediately enclosed in a set function.

The BETWEEN operator cannot be used with a subquery. However, the BETWEEN
operator can be used within the subquery.

You might also like