- Programming language used to manage and manipulate the data within the relational databases.
- Standard language used by most
RDBMS
, i.e.MySQL
,Oracle
,SQL Server
, etc. - Allows
developers
anddatabase administrators
to manage large amount of data effectively. C.R.U.D
:Create
,Retrieve
,Update
andDelete
- Used to
create
,retrieve
,update
,delete
,modify
,filter
,sort
,aggregate
andquery
databases. Join
multiple tables using a shared key.- Organize data in the selected order (
ASC
orDESC
) - SQL
commands
are instructions used to communicate with thedatabase
Syntax
of a particular database may be different ( i.e.TOP
: Microsoft SQL andLIMIT
: MySQL )Data types
may be different.Funtions
might be different based on name or argument types or order of arguments.
Data Definition Language | Data Query Language | Data Manipulation Language | Data Control Language |
---|---|---|---|
Define Data Structure | Retrieve Data from Database | Manipulate Data in Database | Control Access to Data stored in Database |
|
|
|
|
SELECT * <All Columns>
FROM <Table Name>
SELECT <Columns>
FROM <Table>
JOIN <Another Table>
ON <Common Columns>
WHERE <Filter Condition>
GROUP BY <Grouping>
HAVING <Aggregate Filter>
ORDER BY <Column List>
LIMIT <No. of Rows>
- Select Name in Upper Case as Alias
SELECT
UPPER(Name) as UpperCaseName
FROM Employee;
- Fetch
Top
N Employee andOrder By
Salary inDescending
Order
SELECT *
FROM Employee
ORDER BY Salary DESC
LIMIT 5;
- Retrieve Employee First Name and Employee Last Name in a Single Column Employee Full Name with Space.
SELECT
CONCAT(FirstName, ' ', LastName) AS FullName
FROM Employee;
- Retrieve Designation along with Total Salaries Paid for each of them.
SELECT Designation, SUM(Salary)
FROM Employee
GROUP BY Designation;
- Retrieve Name of Employee which includes Name Kiran
SELECT *
FROM Employee
WHERE Name Like 'Kiran%';
- Retrieve Only First Name from FullName
SELECT
SUBSTRING(FullName,0,Charindex(' ',FullName))
FROM Employee;
- Fetch Duplicate Records from Table
SELECT EID, Department, COUNT(*)
FROM Employee
GROUP BY EID, FullName
HAVING COUNT(*) > 1;
- Remove Duplicates
DELETE FROM Employee
WHERE EID IN (SELECT EID FROM Employee
GROUP BY Department
HAVING COUNT(*) > 1);
- Clone Table or Empty Table with Same Structure
CREATE TABLE NewTable
SELECT * FROM OldTable;
SELECT *
INTO NewTable
FROM OldTable
WHERE 0 = 1;
CREATE TABLE NewTable
LIKE OldTable;
INSERT
INTO NewTable
SELECT *
FROM OldTable;
- Fetch Common Records between 2 Tables
SELECT * FROM Table1
INTERSECT
SELECT * FROM Table2;
- Increase Income of all Employees by
5%
in a Table
UPDATE Employee
SET Income = Income + (Income * 0.05);
- Find Names of Employee Starting with
A
SELECT Name
FROM Employee
WHERE Name LIKE 'A%';
- Find Number of Employees working in Department of Data Science
SELECT COUNT(*)
FROM Employee
WHERE Department = 'Data Science';
- Find Details of Employee whose FirstName End with 'A' and contains 6 Alphabets
SELECT * FROM Employee
WHERE FirstName LIKE '______A'
- Find Employees whose Salary lies
between
10,000 and 50,000
SELECT * FROM Employee
WHERE Salary BETWEEN 10000 AND 50000;
- Highest Salary in Department
SELECT ID, MAX(Salary)
FROM Employees
GROUP BY ID;
Select from Multiple Tables
SELECT D.Name AS 'Department', E.Name AS 'Employee', E.Salary
FROM Employee E
INNER JOIN Department D
ON E.ID = D.ID
WHERE (ID, Salary)
IN (SELECT ID, MAX(Salary)
FROM Employees
GROUP BY ID)
- Second Highest Salary
-- MAX:
SELECT MAX(Salary)
FROM Employee
WHERE Salary < (SELECT MAX(Salary) FROM Employee);
-- LIMIT:
SELECT Salary
FROM (SELECT Salary FROM Employee ORDER BY Salary DESC LIMIT 2)
AS EmployeeSalary
ORDER BY Salary LIMIT 1;
-- TOP:
SELECT Top 1 Salary
FROM (SELECT Top 2 Salary FROM Employee ORDER BY Salary DESC)
AS EmployeeSalary
ORDER BY Salary ASC;
- Find all Duplicate Emails
SELECT Email
FROM Employee
GROUP BY Email
HAVING COUNT(Email) > 1;
-- Create temporary table:
SELECT DISTINCT *
INTO NewTable
FROM OldTable;
-- DROP old table:
DROP * FROM OldTable;
-- INSERT into old table from new table:
INSERT INTO OldTable
SELECT * FROM NewTable;
-- DROP new table:
DROP TABLE NewTable;
SELECT * FROM Employee
WHERE Employee_Name IN (SELECT Employee_Name FROM Department);