1631614746411SQL Notes
1631614746411SQL Notes
Table of Contents
What is SQL? .................................................................4
What is data? ................................................................4
What is database? ........................................................4
What is table? ...............................................................5
What is field? ................................................................5
What is record? ............................................................5
How to work with SQL? ................................................6
Create Database ...........................................................6
Select Database ............................................................7
Show Database .............................................................7
Drop Database ..............................................................7
Create Table Statement ................................................7
Drop/Delete Table Statement.......................................8
ALTER TABLE Statement ...............................................8
MYSQL INSERT INTO Statement....................................9
SQL SELECT Statement ................................................10
The SQL WHERE Clause ...............................................10
2|Page
The SQL UPDATE .........................................................11
The SQL DELETE ..........................................................12
The SQL AND OPERATOR ............................................12
The SQL OR OPERATOR ...............................................13
The SQL NOT OPERATOR ............................................13
The SQL LIKE Operator ................................................14
The SQL IN Operator ...................................................15
The SQL BETWEEN Operator.......................................15
The SQL SELECT TOP Clause ........................................16
The SQL ORDER BY Clause ..........................................16
Working with SQL functions .......................................17
SQL Joins .....................................................................17
The SQL GROUP BY .....................................................20
SQL INSERT INTO SELECT Statement ...........................21
The SQL TRUNCATE TABLE Command.........................21
SQL Constraints...........................................................22
3|Page
What is SQL?
SQL (Structured Query Language) is a standard language for storing,
manipulating and retrieving data in databases.
SQL is not a database system, It is language which is used by
database management system like (Oracle, MySQL, Microsoft SQL
Server, MongoDB and more).
It is also pronounced as See-Quell.
SQL language is mainly designed for maintaining the data in
relational database management systems (RDBMS).
Big enterprises like Facebook, Instagram, and LinkedIn, use SQL for
storing the data in the back-end.
Programming languages like php, asp.net and python use sql for
backend.
What is data?
Data is a collection of information.
Data can be name, age, height, phone number, picture, PDF, audio
and more.
What is database?
A database is an electronic storage which is created by DBMS
software's that stores the organized collection of records. It can be
accessed and manage by the user very easily. It allows us to organize
4|Page
data into tables, rows, columns, and indexes to find the relevant
information very quickly.
What is table?
Table is a collection of data which is organized in terms of rows and
columns.
Employee_ID Employee_Name City Mobile Number
1 AJAY Bhopal 1111-2222-33
2 SUNIL Jaipur 3311-2222-33
3 MANOJ Udaipur 2211-2222-33
4 KOMAL SURAT 2211-2222-33
What is field?
A field is a data structure for a single piece of data. For example, in a
table called student contact information, telephone number is a
field in column.
Employee_ID Employee_Name City Mobile Number
1 AJAY Bhopal 1111-2222-33
2 SUNIL Jaipur 3311-2222-33
3 MANOJ Udaipur 2211-2222-33
4 KOMAL SURAT 2211-2222-33
What is record?
In relational databases, a record is a group of related data held
within the same structure.
5|Page
Employee_ID Employee_Name City Mobile Number
1 AJAY Bhopal 1111-2222-33
2 SUNIL Jaipur 3311-2222-33
3 MANOJ Udaipur 2211-2222-33
4 KOMAL SURAT 2211-2222-33
Create Database
Syntax : CREATE DATABASE database_name;
Example : CREATE DATABASE company;
6|Page
Select Database
Syntax : USE database_name;
Example : USE company;
Show Database
Syntax : SHOW DATABASES;
Drop Database
Syntax : DROP DATABASE database_name;
8|Page
Example:-
ALTER TABLE employeeInfo;
DROP Email;
Syntax:
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);
9|Page
INSERT INTO employeeInfo (Emp_name, Address, City, Age, DOJ,
Designation, Salary, Mobile)
VALUES ('Kamal', 'P76, Block No.5', 'Jaipur', 36, '2020-2-1', 'Sr.
Programmer', 60000, '9188833311');
Example: 2
SELECT Emp_name, Mobile, Designation, Salary
FROM employeeInfo
WHERE Salary>45000;
Example: 1
UPDATE employeeInfo
SET Address =
WHERE [condition];
Example: 2
UPDATE employeeInfo
SET Address = 'P79, Block No.5'
WHERE ID=4;
11 | P a g e
Example: 3
UPDATE employeeInfo
SET Salary = 45000
WHERE ID = 5;
Example:
DELETE FROM employeeInfo
WHERE ID = 5;
If you want to delete all records from table then you can use
DELETE FROM TABLE_NAME;
12 | P a g e
Example: 1
SELECT ID, Emp_name
FROM employeeInfo
WHERE Designation = 'Programmer' AND Age > 30;
Example: 2
SELECT ID, Emp_name
FROM employeeInfo
WHERE Designation = 'Programmer' AND City = 'Jaipur';
Example:
SELECT ID, Emp_name, City
FROM employeeInfo
WHERE City = 'Jaipur' OR Salary = 42000;
13 | P a g e
Example:
SELECT ID, Emp_name, City
FROM employeeInfo
WHERE NOT City = 'Jaipur';
Example: 1
SELECT *FROM employeeInfo
WHERE City LIKE 'J%'; (Start With) you can also use (End With Like – %J)
Example: 2
SELECT *FROM employeeInfo
WHERE Mobile LIKE '__8%';
Example: 3
SELECT * FROM employeeInfo
WHERE Emp_name NOT LIKE 'm%';
14 | P a g e
The SQL IN Operator
Example: 1
SELECT * FROM employeeInfo
WHERE City IN ('Udaypur', 'Ajmer');
Example: 2
SELECT * FROM employeeInfo
WHERE City NOT IN ('Udaypur', 'Ajmer');
Example: 2
SELECT * FROM employeeInfo
WHERE ID BETWEEN 3 AND 5;
Example: 3
SELECT * FROM employeeInfo
WHERE DOJ BETWEEN '2021-01-01' AND '2021-05-30';
15 | P a g e
The SQL SELECT TOP Clause
SELECT TOP Clause is used to show specify the number of records
from table.
Note: Not all database systems support the SELECT TOP clause.
MySQL supports the LIMIT clause.
Syntax:
SELECT TOP number|percent column_name(s)
FROM table_name
WHERE [condition];
Example: 1
SELECT * FROM employeeInfo
ORDER BY Emp_name, Salary;
Example: 2
SELECT * FROM employeeInfo
ORDER BY Emp_name DESC;
16 | P a g e
Working with SQL functions
COUNT() Example:
SELECT COUNT(Emp_name)
FROM employeeInfo;
SUM() Example:
SELECT SUM(Salary)
FROM employeeInfo;
AVG() Example:
SELECT AVG(Salary)
FROM employeeInfo;
MIN() Example:
SELECT Emp_name, MIN(Salary) AS LowestSalary
FROM employeeInfo;
MAX() Example:
SELECT Emp_name, MIN(Salary) AS HighestSalary
FROM employeeInfo;
SQL Joins
The SQL Joins clause is used to combine records from two or more
tables.
17 | P a g e
Example:
Step 1 (Create database)
CREATE DATABASE companydb;
19 | P a g e
INNER JOIN QUERY 1
SELECT Orders.OrderID, Customer.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customer ON Orders.CustomerID=Customer.CustomerID;
Some other SQL Joins Like – LEFT JOIN, RIGHT JOIN, FULL JOIN and
SELF JOIN, you can read from Google.
20 | P a g e
Example:
SELECT COUNT(Designation), SUM(Salary), Designation
From employeeInfo
GROUP BY Designation;
Example:
INSERT INTO employeeInfoBackup
SELECT * FROM employeeInfo;
21 | P a g e
SQL Constraints
By using SQL constraints you can set specify rules for the data in a
table.
USE constraintsExample;
22 | P a g e
Step 4 Insert Records
23 | P a g e
DEFAULT CONSTRAINT - Set a default value for a column if no value is
inserting.
Example of DEFAULT Constraints:
24 | P a g e
Step 4 View Table
SELECT * FROM defaultExample;
25 | P a g e
UNIQUE CONSTRAINT - The UNIQUE constraint ensures that all
values in a column are different.
Example of UNIQUE CONSTRAINT
Step 1 Create Table
CREATE TABLE uniqueExample (
EmpName varchar(255) NOT NULL,
Mobile varchar(10) NOT NULL UNIQUE
);
26 | P a g e
SQL FOREIGN KEY CONSTRAINT -
Step 1 Create table
27 | P a g e
Step 4 Insert Records
The End
28 | P a g e