SQL NOTES nidhi
SQL NOTES nidhi
𝐃𝐚𝐭𝐚
Data is a collection of facts, such as numbers, words, measurements,
observations or descriptions of things.
Examples-
o Numbers (e.g., 42,3.14)
o Words (e.g., “Hello”, “SQL”)
o Measurements (e.g., height , weight)
o Observations (e.g., “It is raining”)
Database
A database is an organized collection of data, generally stored and
accessed electronically from a computer system.
It allow for efficient storage, retrieval, and management of data.
Examples of Databases:
o E-commerce platforms (e.g., Amazon)
o Social media platforms (e.g., Facebook)
Database Management System (DBMS)
Database management system is a software which is used to manage the
database.
DBMS provides an interface to perform various operations like-
1. Creating databases, tables, and objects.
2. Inserting, updating, and deleting data.
3. Dropping databases, tables, and objects.
4. Provides data security.
Some popular DBMS softwares are MS SQL
SERVER,Oracle,MySQL,IBM,DB2,PostgreSQL etc.
Table
A table in a database is a collection of rows and columns.
It is used to organize and store data in a structured format.
Row and Column
A row is a horizontal arrangement of data moving from right to left. It is
often referred to as a record or a tuple. It represents individual data
entries.
A column is a vertical arrangement of data moving from top to bottom. It
is often referred to as an attribute or a field. It represents the
characteristics or properties of the data.
Example- A "Customers" table might have columns like CustomerID, Name,
Email, and Phone with each row representing a different customer.
SQL
SQL stands for Structured Query Language was initially developed by
IBM.
Initially it was called as SEQUEL (Structure English Query Language)
SQL is a programming language used to interact with database.
SQL allows you to create, read, update, and delete (CRUD) data in a
relational database.
SQL Data Types
Data Types define the type of data that can be stored in a table column.
It ensures data integrity and optimizes storage.
1. CHAR-
Can store characters of fixed length.
The size parameter specifies the column length in characters can
be from 0 to 255.
2. VARCHAR-
Can store characters up to given length.
The size parameter specifies the maximum column length in
characters can be from 0 to 65535.
1. INT-
Used for storing whole numbers without decimal.
2. FLOAT-
Used for storing numbers without decimal.
Float gives approximate value while performing calculations.
D. BIT Datatype- BIT data type can store either True or False values
(represents by 1 and 0).
Example-
CREATE TABLE Employees (
EmployeeID INT,
FirstName VARCHAR(50),
LastName CHAR(20),
Salary DECIMAL(10, 2),
BirthDate DATE,
HireTime TIME,
IsActive BIT
);
SQL Commands
1. Create a Database-
Syntax-
Example-
Syntax-
USE database_name;
Example-
USE SalesDB;
2. Create Table-
Syntax-
Example-
1. Drop a Database-
Syntax-
Example-
2. Drop a Table-
Syntax-
Example-
DROP TABLE CUSTOMERS;
Insert Command
Syntax-
Example-
INSERT INTO CUSTOMERS (ID, NAME, AGE, ADDRESS, SALARY)
VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00);
INSERT INTO CUSTOMERS (ID, NAME, AGE, ADDRESS, SALARY)
VALUES (2, 'Khilan', 25, 'Delhi', 1500.00);
You can create a record in the CUSTOMERS table by using the second syntax as
shown below.
NOTE- You may not need to specify the column(s) name in the SQL query if you
are adding values for all the columns of the table. But make sure the order of
the values is in the same order as the columns in the table.
Syntax-
Example-
Syntax-
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Example-
To select all columns from a table, you can use the asterisk (*):
Syntax-
Example-
Syntax-
Example-
To fetch the ID, Name and Salary fields of the customers available in
CUSTOMERS
table.
SELECT ID, NAME, SALARY FROM CUSTOMERS;
Where Clause
Syntax-
Example-
To fetch the ID, Name and Salary fields from the CUSTOMERS table for a
customer with the name Hardik.
Note- It is important to note that all the strings should be given inside single
quotes (''). Whereas, numeric values should be given without any quote
SELECT ID, NAME, SALARY
FROM CUSTOMERS
WHERE NAME = 'Hardik';
Example-
To fetch the ID, Name and Salary fields from the CUSTOMERS table, where the
salary is greater than 2000.
SELECT *
FROM CUSTOMERS
WHERE SALARY > 2000;
SQL Operators
SQL operators are used to perform operations on data within queries.
SQL operators are symbols or keywords that perform actions like
comparing values, doing math or combining conditions in queries to
filter, manipulate and analyze data in databases.
1. Arithmetic Operators-
Used to perform mathematical operations on numeric values.
Common operators-
SELECT 10 + 5;
SELECT 10 - 5;
SELECT 10 * 5;
SELECT 10 / 5;
SELECT 10 % 5;
Common operators-
a) Equal (=): Checks if two values are equal, if yes then condition
Example-
becomes true.
b) Not equal to (!= or <>): Checks if two values are not equal, if yes then
Example-
condition becomes true.
Example-
c) Greater than (>): Checks if one value is greater than another.
Example-
equal to another.
f) Less than or Equal To (<=): Checks if one value is less than or equal to
Example-
another.
Common Operators-
Syntax-
a) AND: Returns true if all conditions are true.
Example-
To fetch the ID, Name and Salary fields from the CUSTOMERS table, where the
salary is greater than 2000 and the age is less than 25 years.
Syntax-
b) OR: Returns true if any condition is true.
Example-
To fetch the ID, Name and Salary fields from the CUSTOMERS table, where the
salary is greater than 2000 OR the age is less than 25 years.
SELECT ID, NAME, SALARY
FROM CUSTOMERS
WHERE SALARY > 2000 OR age < 25;
Syntax-
not true.
Example-
To fetch the ID, Name and Salary fields from the CUSTOMERS table, whose
salary is not 2000.
Syntax-
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
Example-
Retrieve the ID, name, and address of customers living in either Mumbai, Delhi,
or Bhopal.
Syntax-
SELECT column1, column2, ...
FROM table_name
WHERE column_name BETWEEN low_value AND high_value;
Example-
Retrieve ID, NAME, AGE FROM CUSTOMERS WHERE AGE BETWEEN 24 AND 27
from the CUSTOMERS table.
SELECT *
FROM CUSTOMERS
WHERE SALARY BETWEEN 2000 AND 10000;
NUL
NULL values represent missing value or unknown data.
NULL is neither a space or zero(0).
NULL can’t be compared with any value/anything, not even to another
NULL.
Example-
If you have a CUSTOMERS table and you want to insert a record for Bharti
without providing her age and address:
INSERT INTO CUSTOMERS (ID, NAME, SALARY)
VALUES (7, 'Bharti', 12000.00 );
In this case, if the AGE AND ADDRESS column is set to default to NULL, BHARTI
AGE AND ADDRESS value will be NULL.
Example-
If you do not want to provide a value for a column, you can assign NULL to that
column.
In this statement, we are inserting a new record into the CUSTOMERS table.
The ID is set to 8, the NAME is set to 'Rahul', AGE is set to 25, ADDRESS is set to
'Delhi' and the SALARY is set to NULL.
Syntax-
SELECT * FROM table_name WHERE column_name IS NULL;
Example-
Suppose you have a CUSTOMERS table, and you want to find all customers
whose salary is not provided (i.e., NULL):
SELECT * FROM CUSTOMERS WHERE SALARY IS NULL;
IS NOT NULL
This condition is used to check whether a column does not contain a NULL
value.
Syntax:
SELECT * FROM table_name WHERE column_name IS NOT NULL;
Example-
Using the same CUSTOMERS table, if you want to find all customers who have a
salary specified (i.e., not NULL):
SELECT * FROM CUSTOMERS WHERE SALARY IS NOT NULL;
DISTIN
Syntax-
SELECT DISTINCT column1, column2, ...
FROM table_name;
Example-
Suppose you have a table called Customers with the following data:
If you want to retrieve distinct address from this table, you can use the
DISTINCT keyword like this:
SELECT DISTINCT ADDRESS
FROM CUSTOMERS;
If you want to select all unique rows from a table, you can use:
Syntax-
SELECT DISTINCT *
FROM table_name;
Note- This will return all columns from the table but only show distinct rows.
However, keep in mind that using DISTINCT * may not be efficient if your table
has many columns. It's usually better to specify only the columns you need.
Alia
Syntax-
The basic syntax of a table alias is as follows-
SELECT column_name AS alias_name
FROM table_name
WHERE [condition];
Result-
Here,
Customer_Name is the alias for the NAME column from the CUSTOMERS
table.
Order_Amount is the alias for the AMOUNT column from the ORDERS
table.
Order_Date is the alias for the DATE column from the ORDERS table.
Result-
In SQL Server, the AS keyword is commonly used to create aliases, it is not
mandatory. You can create aliases without using AS by simply specifying the
alias name immediately after the column or table name.
1. Column Alias: Just place the alias name after the column name.
Syntax-
SELECT column_name alias_name;
2. Table Alias: Just place the alias name after the table name.
Syntax-
SELECT column_name FROM table_name alias_name;
Example- Here’s a full query using both types of aliases without the
AS keyword.
1. SELECT INTO-
SELECT INTO is used to create a new table and copy data into it at the
same time.
It duplicates both the structure and data from an existing table into a
new one.
Example-
SELECT * INTO BACKUP_CUSTOMERS
FROM CUSTOMERS
WHERE ADDRESS = 'Delhi';
This will create a new table backup_customers and copy all rows from the
customers table where address= 'Delhi'.
B) Copy only Selected or Few Columns
into a New Table-
Syntax-
SELECT columns
INTO new_table
FROM existing_table
WHERE condition(Optional);
Example-
SELECT NAME, ADDRESS INTO BACKUP_CUSTOMERSDETAIL
FROM CUSTOMERS;
Syntax-
INSERT INTO target_table (column1, column2, ...)
SELECT column1, column2, ...
FROM source_table
WHERE condition;
Example-
INSERT INTO SELECT statement to copy data from CUSTOMERS to
BACKUP_CUSTOMERS for customers who are older than 30.
Here,
INSERT INTO BACKUP_CUSTOMERS: This specifies that you are
inserting data into the BACKUP_CUSTOMERS table.
(ID, NAME, AGE, ADDRESS, SALARY): These are the columns in the
BACKUP_CUSTOMERS table that will receive the data.
SELECT ID, NAME, AGE, ADDRESS, SALARY FROM CUSTOMERS: This
selects the corresponding columns from the CUSTOMERS table.
WHERE AGE > 30: This condition ensures that only rows where the
AGE is greater than 30 are copied from CUSTOMERS to
BACKUP_CUSTOMERS.
B) Copy All Columns from one Table to
Another Table-
Syntax-
INSERT INTO target_table
SELECT *
FROM source_table
WHERE condition;
Example-
INSERT INTO BACKUP_CUSTOMERS
SELECT *
FROM CUSTOMERS
WHERE ADDRESS ='Delhi';