SQL Notes
Date: 2024-01-07
Time: 18:40
Introduction
Structured query language (SQL) is a programming language for storing and processing information in a
relational database. A relational database stores information in tabular form, with rows and columns
representing different data attributes and the various relationships between the data values.
DDL (Data Definition Language)
DDL or Data Definition Language actually consists of the SQL commands that can be used to define the
database schema. It simply deals with the description of the database schema and is used to create and modify
the structure of the database objects in the database.
These commands are normally not used by a general user, who should be accessing the database via the
application
Commands :
CREATE, DROP, ALTER, TRUNCATE, COMMENT, RENAME
DML (Data Manipulation Language)
The SQL commands that deal with the manipulation of data present in the database belong to DML or Data
Manipulation Language and this includes most of the SQL statements. It is the component of the SQL statement
that controls access to data and to the database.
Basically, DCL statements are grouped with DML statements.
Commands :
INSERT, UPDATE, DELETE, LOCK, CALL, EXPLAIN PLAN
DCL (Data Control Language)
DCL includes commands such as GRANT and REVOKE which mainly deal with the rights, permissions, and
other controls of the database system.
GRANT : This command gives users access privileges to the database
REVOKE : This command revokes the user's access privileges given by using the GRANT command
SQL Data Types
A column’s data type is essentially the type of data format that will be used to store the data in each cell.
For every database, data types are primarily classified into three categories.
1. Numeric Datatypes
2. Date and Time Datatypes
3. String Datatypes
Commands
SELECT
The SELECT statement is used to select data from a database.
Syntax :
SELECT column1,column2,...
FROM table_name;
SELECT DISTINCT
The SELECT DISTINCT statement is used to return only distinct values.
Syntax :
SELECT DISTINCT column1,column2,...
FROM table_name;
SQL Where Clause
The Where clause is used to filter records.
Syntax :
SELECT * FROM table_name WHERE condition;
SQL AND
The WHERE clause can contain one or many AND operators.
Syntax :
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
SQL OR
The WHERE clause can contain one or more OR operators.
Syntax :
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
SQL IN
The IN operator allows you to specify multiple values in a WHERE clause.
The IN operator is a shorthand for multiple OR conditions
Syntax :
SELECT column_name(s)
FROM table_name
WHERE column_name_ IN (value1, value2, ...);
SQL Between
The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates.
Syntax :
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
SQL Like
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
Syntax :
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
SQL Order By
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
Syntax :
SELECT column1_, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
SQL Aggregate Functions
COUNT counts how many rows are in a particular column.
SUM adds together all the values in a particular column.
MIN and MAX return the lowest and highest values in a particular column, respectively.
AVG calculates the average of a group of selected values.
SQL Group By
The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of
customers in each country".
The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to
group the result-set by one or more columns
Syntax :
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
CREATE & DROP TABLE
The CREATE DATABASE statement is used to create a new SQL database.
Syntax : CREATE DATABASE databasename;
The DROP DATABASE statement is used to drop an existing SQL database.
Syntax : DROP DATABASE databasename
SQL Constraints
Constraints can be specified when the table is created with the CREATE TABLE statement, or after the table is
created with the ALTER TABLE statement.
Syntax :
CREATE TABLE _table_name_ (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);
INSERT, UPDATE, DELETE, ALTER
The INSERT INTO statement is used to insert new records in a table.
The UPDATE statement is used to modify the existing records in a table.
The DELETE statement is used to delete existing records in a table.
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table
NULL Values
A field with a NULL value is a field with no value.
If a field in a table is optional, it is possible to insert a new record or update a record without adding a value to
this field. Then, the field will be saved with a NULL value.