SQL v1 PDF
SQL v1 PDF
(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 Database most often contains one or more tables. Each table is identified by a
name – “Customers”, “Orders”.
CUSTOMER_NAME ORDERS_NAME
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:
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.
Drop Delete
Rename Merge
Truncate
Comment
Create
CREATE DATABASE is the SQL command for creating a database.
Tables can be created using CREATE TABLE statement and it has the
following syntax.
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.
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
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
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
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
LIMIT count;
Thorough SQL
Retrieving , Restricting and sorting data(DQL)
Basic select statement with arithmetic operators
UPDATE Statement
DELETE Statement
TRUNCATE Statement
Left Join Right Join
Table A Table B
Joins
Table A Table B
Table A Table B
Table A Table B
Table A Table B
Table A Table B
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
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 −
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.
The BETWEEN operator cannot be used with a subquery. However, the BETWEEN
operator can be used within the subquery.