Skip to content

iamkirankumaryadav/SQL

Repository files navigation

Navigate to C#

SQL - Structured Query Language

  • 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 and database administrators to manage large amount of data effectively.
  • C.R.U.D: Create, Retrieve, Update and Delete
  • Used to create, retrieve, update, delete, modify, filter, sort, aggregate and query databases.
  • Join multiple tables using a shared key.
  • Organize data in the selected order ( ASC or DESC )
  • SQL commands are instructions used to communicate with the database

Difference in databases

  • Syntax of a particular database may be different ( i.e. TOP : Microsoft SQL and LIMIT : MySQL )
  • Data types may be different.
  • Funtions might be different based on name or argument types or order of arguments.

SQL Categories | Subset of SQL

DDL

DQL

DML

DCL

Data Definition LanguageData Query LanguageData Manipulation LanguageData Control Language
Define Data StructureRetrieve Data from DatabaseManipulate Data in DatabaseControl Access to Data stored in Database
  1. CREATE
  2. ALTER
  3. DROP
  4. RENAME
  5. TRUNCATE
  6. COMMENT
  1. SELECT
  1. INSERT
  2. UPDATE
  3. DELETE
  4. MERGE
  1. GRANT
  2. REVOKE

Most Basic and Common Queries:

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>
  1. Select Name in Upper Case as Alias
SELECT
UPPER(Name) as UpperCaseName
FROM Employee;
  1. Fetch Top N Employee and Order By Salary in Descending Order
SELECT * 
FROM Employee
ORDER BY Salary DESC
LIMIT 5;
  1. 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;
  1. Retrieve Designation along with Total Salaries Paid for each of them.
SELECT Designation, SUM(Salary)
FROM Employee
GROUP BY Designation;
  1. Retrieve Name of Employee which includes Name Kiran
SELECT * 
FROM Employee
WHERE Name Like 'Kiran%';
  1. Retrieve Only First Name from FullName
SELECT 
SUBSTRING(FullName,0,Charindex(' ',FullName))
FROM Employee;
  1. Fetch Duplicate Records from Table
SELECT EID, Department, COUNT(*)
FROM Employee
GROUP BY EID, FullName
HAVING COUNT(*) > 1;
  1. Remove Duplicates
DELETE FROM Employee
WHERE EID IN (SELECT EID FROM Employee
              GROUP BY Department
              HAVING COUNT(*) > 1);
  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;
  1. Fetch Common Records between 2 Tables
SELECT * FROM Table1
INTERSECT
SELECT * FROM Table2;
  1. Increase Income of all Employees by 5% in a Table
UPDATE Employee
SET Income = Income + (Income * 0.05);
  1. Find Names of Employee Starting with A
SELECT Name 
FROM Employee
WHERE Name LIKE 'A%';
  1. Find Number of Employees working in Department of Data Science
SELECT COUNT(*) 
FROM Employee
WHERE Department = 'Data Science';
  1. Find Details of Employee whose FirstName End with 'A' and contains 6 Alphabets
SELECT * FROM Employee
WHERE FirstName LIKE '______A'
  1. Find Employees whose Salary lies between 10,000 and 50,000
SELECT * FROM Employee
WHERE Salary BETWEEN 10000 AND 50000;
  1. 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)
  1. 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;
  1. Find all Duplicate Emails
SELECT Email
FROM Employee
GROUP BY Email
HAVING COUNT(Email) > 1;

Drop Duplicates from Table

-- 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;

Compare Two Columns

SELECT * FROM Employee
WHERE Employee_Name IN (SELECT Employee_Name FROM Department);