3) The remaining rows after the WHERE constraints are applied are then grouped based on common values in the column specified in the GROUP BY clause.
4) If the query has a GROUP BY clause, then the constraints in the HAVING clause are then applied to the grouped rows, discard the grouped rows that don't satisfy the constraint.
5) Any expressions in the SELECT part of the query are finally computed.
6) Of the remaining rows, rows with duplicate values in the column marked as DISTINCT will be discarded.
7) If an order is specified by the ORDER BY clause, the rows are then sorted by the specified data in either ascending or descending order.
8) Finally, the rows that fall outside the range specified by the LIMIT and OFFSET are discarded, leaving the final set of rows to be returned from the query.
------------------------------------------------------------------------------------------------------------------------
Constraints in SQL:
- NOT NULL - Ensures that a column cannot have a NULL value
- UNIQUE - Ensures that all values in a column are different
- PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table
- FOREIGN KEY - Prevents actions that would destroy links between tables
- CHECK - Ensures that the values in a column satisfies a specific condition
- DEFAULT - Sets a default value for a column if no value is specified
- CREATE INDEX - Used to create and retrieve data from the database very quickly.
---------------------------------------------------------------------------------------------------------------------
What Can SQL do?
SQL can execute queries against a database
SQL can retrieve data from a database
SQL can insert records in a database
SQL can update records in a database
SQL can delete records from a database
SQL can create new databases
SQL can create new tables in a database
SQL can create stored procedures in a database
SQL can create views in a database
SQL can set permissions on tables, procedures, and views
------------------------------------------------------------------------------------------------------------------------
RDBMS
RDBMS stands for Relational Database Management System.
RDBMS is the basis for SQL, and for all modern database systems such as MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access.
The data in RDBMS is stored in database objects called tables.
A table is a collection of related data entries and it consists of columns and rows.
Some of The Most Important SQL Commands
SELECT - extracts data from a database
UPDATE - updates data in a database
DELETE - deletes data from a database
INSERT INTO - inserts new data into a database
CREATE DATABASE - creates a new database
ALTER DATABASE - modifies a database
CREATE TABLE - creates a new table
ALTER TABLE - modifies a table
DROP TABLE - deletes a table
CREATE INDEX - creates an index (search key)
DROP INDEX - deletes an index
SQL SELECT Statement
The SELECT statement is used to select data from a database.
The result is stored in a result table, called the result-set.
SQL SELECT Syntax
SELECT column_name,column_name
FROM table_name;
and
SELECT * FROM table_name;
Eg: SELECT EmployeeName,City FROM Employees;
Eg:SELECT * FROM Employees;
The SQL SELECT DISTINCT Statement
In a table, a column may contain many duplicate values; and sometimes you only want to list the different (distinct) values.
The DISTINCT keyword can be used to return only distinct (different) values.
SQL SELECT DISTINCT Syntax
SELECT DISTINCT column_name,column_name
FROM table_name;
Eg: SELECT DISTINCT City FROM Employees;
The SQL WHERE Clause
The WHERE clause is used to extract only those records that fulfill a specified criterion.
SQL WHERE Syntax
SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;
Eg: SELECT * FROM Employees
WHERE EmployeeID=1508;
Operators in The WHERE Clause
The following operators can be used in the WHERE clause:
Operator Description
= Equal
<> Not equal. Note: In some versions of SQL this operator may be written as !=
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN To specify multiple possible values for a column
The SQL AND & OR Operators
The AND operator displays a record if both the first condition AND the second condition are true.
The OR operator displays a record if either the first condition OR the second condition is true.
Eg: for AND operator
SELECT * FROM Employees
WHERE City='Nanded'
AND City='Thane';
Eg: for OR operator
SELECT * FROM Emplyees
WHERE City='Bhandup'
OR City='Dadar';
Eg: for both AND and OR
SELECT * FROM Employees
WHERE City='Mulund'
AND (City='CST' OR City='Badlapur');
The SQL ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set by one or more columns.
The ORDER BY keyword sorts the records in ascending order by default.
To sort the records in a descending order, you can use the DESC keyword.
SQL ORDER BY Syntax
SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name ASC|DESC;
Eg: SELECT * FROM Employees
ORDER BY City,EmployeeName;
The SQL INSERT INTO Statement
The INSERT INTO statement is used to insert new records in a table.
SQL INSERT INTO Syntax
It is possible to write the INSERT INTO statement in two forms.
The first form does not specify the column names where the data will be inserted, only their values:
INSERT INTO table_name
VALUES (value1,value2,value3,...);
The second form specifies both the column names and the values to be inserted:
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
Eg: INSERT INTO Employees
VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');
or this SQL statement (including column names):
Eg :INSERT INTO Employees (EmployeeName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');
Eg: INSERT INTO Employees (EmployeeName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');
**The EmployeeID column is an AutoNumber field and is automatically updated with a unique number
for each record in the table.
AutoNumber is a type of data used in Microsoft Access tables to generate an automatically incremented
numeric counter. The default AutoNumber type has a start value of 1 and an increment of1.
The SQL UPDATE Statement
The UPDATE statement is used to update existing records in a table.
SQL UPDATE Syntax
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
Eg: UPDATE Employees
SET ContactName='Alfred Schmidt', City='Hamburg'
WHERE EmployeerName='Alfreds Futterkiste';
The SQL DELETE Statement
The DELETE statement is used to delete rows in a table.
SQL DELETE Syntax
DELETE FROM table_name
WHERE some_column=some_value;
Eg: DELETE FROM Employees
WHERE EmployeeName='Alfreds Futterkiste' AND ContactName='Maria Anders';
Delete All Data
It is possible to delete all rows in a table without deleting the table.
This means that the table structure, attributes, and indexes will be intact:
DELETE FROM table_name;
or
DELETE * FROM table_name;
------------------------------------------------------------------------------------------------------------------------
The SQL SELECT TOP Clause
The SELECT TOP clause is used to specify the number of records to return.
The SELECT TOP clause can be very useful on large tables with thousands of records.
Returning a large number of records can impact on performance.
Note: Not all database systems support the SELECT TOP clause.
SQL Server / MS Access Syntax
SELECT TOP number|percent column_name(s)
FROM table_name;
SQL SELECT TOP Equivalent in MySQL and Oracle
MySQL Syntax
SELECT column_name(s)
FROM table_name
LIMIT number;
Example
SELECT *
FROM Persons
LIMIT 5;
Oracle Syntax
SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;
Example
SELECT *
FROM Persons
WHERE ROWNUM <=5;
Eg: SELECT TOP 2 * FROM Employees;
Eg: SELECT TOP 50 PERCENT * FROM Employees;
------------------------------------------------------------------------------------------------------------------------
The SQL LIKE Operator
The LIKE operator is used to search for a specified pattern in a column.
The "%" sign is used to define wildcards (missing letters) both before and after the pattern.
SQL LIKE Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
Eg: SELECT * FROM Employees
WHERE City LIKE 's%';--------selects all customers with a City starting with the letter "s"
Eg : SELECT * FROM Employees
WHERE City LIKE '%s';--------selects all customers with a City ending with the letter "s"
Eg: SELECT * FROM Employees
WHERE Country LIKE '%land%';-------selects all customers with a Country containing the pattern "land"
Eg: SELECT * FROM Employees
WHERE Country NOT LIKE '%land%';---selects all customers with a Country NOT containing the pattern "land"
------------------------------------------------------------------------------------------------------------------------
The IN Operator
The IN operator allows you to specify multiple values in a WHERE clause.
SQL IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)
---------------------------------------------------------------------
Nth highest salary:
SELECT name, salary
FROM Employee e1
WHERE N-1 = (SELECT COUNT(DISTINCT salary) FROM Employee e2 WHERE e2.salary > e1.salary)
3rd highest means two salaries are higher than it, similarly Nth highest salary means N-1 salaries are higher than it.
4th highest salary:
SELECT Max(Salary)
FROM Employee LIMIT 3,1
---------------------------------------------------------------------
SELF JOIN:
Find customers from the same city?
SELECT A.CustomerName AS CustomerName1, B.CustomerName AS CustomerName2, A.City
FROM Customers A, Customers B
WHERE A.CustomerID <> B.CustomerID
AND A.City = B.City
ORDER BY A.City;
-----
In this example, the ManagerID
column references the EmployeeID
of the manager for each employee. Now, suppose you want to retrieve a list of employees along with their manager's name. You can use a self-join for this:
SELECT
e.EmployeeID,
e.FirstName,
e.LastName,
m.FirstName AS ManagerFirstName,
m.LastName AS ManagerLastName
FROM
employees e
LEFT JOIN
employees m ON e.ManagerID = m.EmployeeID;
---------------------------------------------------------------------
Delete Duplicate Records:
WITH CTE AS (
SELECT
EmployeeID,
ROW_NUMBER() OVER (PARTITION BY DepartmentID ORDER BY EmployeeID) AS RowNum
FROM
employee
)
DELETE FROM employee
WHERE EmployeeID IN (SELECT EmployeeID FROM CTE WHERE RowNum > 1);
-----------------------------------------------------------------------
Validate schema of the table:
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = 'Employee'
-----------------------------------------------------------------------
Copy data from one table to another
INSERT INTO my_schema.department_backup
SELECT * FROM my_schema.department
-----------------------------------------------------------------------
Comparison using tables in another database
select mediavehicleprintinterestsourcesystemuniquekey, mediavehicleprintinterestname
FROM OPENROWSET(
BULK 'https://neustacv2mdsdev01.dfs.core.windows.net/datalake/uk_matrix_datamart/media_vehicle_print_interest/**',
FORMAT='PARQUET'
) d
except
select mediavehicleprintinterestsourcesystemuniquekey, mediavehicleprintinterestname
FROM uk_matrix_datamart.media_vehicle_print_interest
-----------------------------------------------------------------------------------------------------------
Adding million records in the table
DECLARE @Counter INT = 64011 + 1;
DECLARE @EndCounter INT = @Counter + 1524; -- Add 1 million records
WHILE @Counter <= @EndCounter
BEGIN
INSERT INTO dbo.AK_View2_Media_Agency (MEDIA_AGENCY_ID, EnglishEntityName)
VALUES (@Counter, 'NewMedia'); -- Adjust 'YourDataHere' with the actual data
SET @Counter = @Counter + 1;
END;
-----------------------------------------------------------------------------------------------------------