All You To Know: To Become A Successful Data Professional
All You To Know: To Become A Successful Data Professional
Brainalyst is a pioneering data-driven company dedicated to transforming data into actionable insights and
innovative solutions. Founded on the principles of leveraging cutting-edge technology and advanced analytics,
Brainalyst has become a beacon of excellence in the realms of data science, artificial intelligence, and machine
learning.
OUR MISSION
At Brainalyst, our mission is to empower businesses and individuals by providing comprehensive data solutions
that drive informed decision-making and foster innovation. We strive to bridge the gap between complex data and
meaningful insights, enabling our clients to navigate the digital landscape with confidence and clarity.
WHAT WE OFFER
• Data Strategy Development: Crafting customized data strategies aligned with your business
objectives.
• Advanced Analytics Solutions: Implementing predictive analytics, data mining, and statistical
analysis to uncover valuable insights.
• Business Intelligence: Developing intuitive dashboards and reports to visualize key metrics and
performance indicators.
• Machine Learning Models: Building and deploying ML models for classification, regression,
clustering, and more.
• Natural Language Processing: Implementing NLP techniques for text analysis, sentiment analysis,
and conversational AI.
• Computer Vision: Developing computer vision applications for image recognition, object detection,
and video analysis.
• Workshops and Seminars: Hands-on training sessions on the latest trends and technologies in
data science and AI.
• Customized Training Programs: Tailored training solutions to meet the specific needs of
organizations and individuals.
2021-2024
4. Generative AI Solutions
As a leader in the field of Generative AI, Brainalyst offers innovative solutions that create new content and
enhance creativity. Our services include:
• Content Generation: Developing AI models for generating text, images, and audio.
• Creative AI Tools: Building applications that support creative processes in writing, design, and
media production.
• Generative Design: Implementing AI-driven design tools for product development and
optimization.
OUR JOURNEY
Brainalyst’s journey began with a vision to revolutionize how data is utilized and understood. Founded by
Nitin Sharma, a visionary in the field of data science, Brainalyst has grown from a small startup into a renowned
company recognized for its expertise and innovation.
KEY MILESTONES:
• Inception: Brainalyst was founded with a mission to democratize access to advanced data analytics and AI
technologies.
• Expansion: Our team expanded to include experts in various domains of data science, leading to the
development of a diverse portfolio of services.
• Innovation: Brainalyst pioneered the integration of Generative AI into practical applications, setting new
standards in the industry.
• Recognition: We have been acknowledged for our contributions to the field, earning accolades and
partnerships with leading organizations.
Throughout our journey, we have remained committed to excellence, integrity, and customer satisfaction.
Our growth is a testament to the trust and support of our clients and the relentless dedication of our team.
Choosing Brainalyst means partnering with a company that is at the forefront of data-driven innovation. Our
strengths lie in:
• Expertise: A team of seasoned professionals with deep knowledge and experience in data science and AI.
• Customer Focus: A dedication to understanding and meeting the unique needs of each client.
• Results: Proven success in delivering impactful solutions that drive measurable outcomes.
JOIN US ON THIS JOURNEY TO HARNESS THE POWER OF DATA AND AI. WITH BRAINALYST, THE FUTURE IS
DATA-DRIVEN AND LIMITLESS.
2021-2024
Preface
Welcome to “SQL Basic to Advance.” This book is the culmination of my experiences and insights into SQL
(Structured Query Language), crafted not only as an educational resource but also as a practical guide for
anyone eager to learn and master SQL. My name is Nitin Sharma, and I have collaborated with Brainalyst, a
data-driven company, to bring you a comprehensive journey through the realms of SQL, from its basic syntax
to the complex operations that fuel big data analysis and database management.
Journey to Writing This Book
The genesis of this book lies in the numerous queries, challenges, and learning curves I encountered during
my tenure as a database analyst and later, as a data consultant. Throughout my career, I have witnessed the
transformative impact of SQL in various domains—from small enterprises to large-scale organizations, SQL
remains a critical skill that powers data management and insightful analytics.
Teaming up with Brainalyst, a company at the forefront of data innovation, provided the perfect partnership
to develop a book that not only teaches SQL but also aligns with the needs of modern, data-driven businesses.
Our goal was to create a guide that is both instructive and insightful, tailored to assist learners and profession-
als in navigating the complexities of database environments with ease and confidence.
What to Expect
This book is structured to guide you through a logical progression, starting with the fundamental principles
of SQL and advancing through to more sophisticated techniques. It is replete with examples, exercises, and
real-world scenarios that not only elucidate complex concepts but also enhance your problem-solving skills.
Whether you are a student stepping into the world of databases, a professional seeking to bolster your tool-
kit, or a curious mind looking to understand how data manipulation works at scale, this book aims to be your
companion through your SQL mastery.
Acknowledgments
I am immensely grateful to the entire team at Brainalyst for their support and contributions. Their insights
and industry experience have been invaluable in ensuring that the content is both current and relevant. Ad-
ditionally, I extend my thanks to the community of SQL professionals and enthusiasts whose feedback and
suggestions have helped shape this book into a resource that caters to learners from diverse backgrounds.
Copyright
This book is published with the permission of Brainalyst and is protected under copyright laws from 2021 to
2024. All rights are reserved, and no part of this book may be reproduced, stored in a retrieval system, or
transmitted in any form or by any means—electronic, mechanical, photocopying, recording, or otherwise—
without prior written permission from the publisher, except for brief quotations embedded in critical articles
or reviews.
Moving Forward
As you embark on your journey through the pages of this book, I hope you find it both enlightening and en-
gaging. SQL is a powerful tool in the arsenal of data management, and mastering it can open up numerous
opportunities for innovation and career advancement. Happy learning!
Nitin Sharma
Author and Data Enthusiast
2021-2024
TABLE OF CONTENTS
1. Introduction 9. Using SQL in Applications
• Understanding SQL and Its Importance • Integrating SQL with Programming Languages
• History of SQL • Real-world Applications of SQL
Disclaimer: This material is protected under copyright act Brainalyst © 2021-2024. Unauthorized use and/ or duplica-
tion of this material or any part of this material including data, in any form without explicit and written permission from
Brainalyst is strictly prohibited. Any violation of this copyright will attract legal actions.
2021-2024
Introduction to SQL: Harnessing the Power of Structured Query Language
Welcome to “SQL Basic to Advance,” a comprehensive guide designed to empower you with a deep understanding of SQL
(Structured Query Language), the essential tool for managing and manipulating relational databases. Whether you are a be-
ginner seeking foundational skills or an experienced professional aiming to enhance your database management capabilities,
this book is crafted to facilitate your mastery of SQL.
2021-2024
BRAINALYST - SQL SERIES
SQL BASIC TO ADVANCE
History of SQL:
SQL- (structured query Language), has a history created in the 1970s when IBM developed the System R project.
It aimed to create a database management system that could handle and manipulate structured data efficiently.
As part of system R, IBM introduced a language called Structured English query language (Sequel), which later
evolved into SQL the name ‘SEQUEL’ was already taken by another company. the creators of the language didn’t
want to get sued, so they changed the name to ‘SQL’. Sql is a backronym for “Structured query language.’ the lan-
guage was influenced by edgar F. Codd’s relational model, which introduced the concept of organizing data into
tables with relationships. over time SQL became the standard language for interacting with relational databases
and was standardized by ANSI and ISO. Today, Sql is widely adopted and utilized in various database systems,
enabling organizations to manage and analyze structured data effectively.
LETS FIRST UNDERSTAND THE DATA MODELING AND DATABASE:
Data Modeling: What is it?
Creating data models to defining data structure, creating data models to describe associations and con-
straints for reuse. Data model is the conceptual design or plan for organizing data. It visually represents
data with diagrams, symbols, or text to visualize relationships.
Enhancing data analytics data modeling assures uniformity in nomenclature, rules, semantics, and security.
Regardless of the application the emphasis is on the arrangement and accessibility of the data.
Advantages of data modelling
The following are the essential advantages of Data modelling:
• Data model helps us choose the right data sources to populate the model.
• Data model improves communication throughout the company.
• Data model aids in the ETL process documentation of the data mapping.
• We can use data modeling to query the database’s data and generate various reports based on
the data. Data modeling supports data analysis through reports.
Terminology used in Data Modeling:
• Entity: Entities are the objects or items in the business environment for which we need to store
data. Defines what the table is about. Example- in an e-commerce system, entities can be customers,
orders, or products.
• Attribute: Attributes provide a way to structure and organize the data within an entity. they rep-
resent the specific characteristics or properties of an entity. For instance, attributes of a Customer
entity can include Name, Address, and Email.
• Relationship: Relationships specify the connections are connected or associations with between
entities. They explain the interactions and dependencies between entities. Example- the relation-
ship between Customers and orders represents that a customer can place multiple orders.
• Reference table: Reference table is used to resolve many-to-many relationships between entities. It
transforms the relationship into one-to-many or many-to-one relationships. Example- system with
customers and Products, a reference table called Orderdetails can be used to link customers
• Database logical design: It refers to designing the database within a specific data model of a
database management system. It involves creating the structure, relationships, and rules of the
database at a conceptual level.
• Logical design: Logical design involves the creation of keys, tables, rules, and constraints within
the database. It focuses on the logical structure and organization of data without considering
specific implementation details.
• Database physical design: It encompasses the decisions related to file organization, storage
design, and indexing techniques for efficient data storage and retrieval within the database.
• Physical model: The physical model is a representation of the database that considers imple-
mentation-specific details such as file formats, storage mechanisms, and index structures. trans-
lates the logical design into an actual database implementation.
• Schema: schema refers to the complete description or blueprint of the database. It defines the
structure, relationships, constraints, and permissions associated with the database objects.
• Logical schema: logical schema represents the theoretical design of a database. It is typically
created during the initial stages of database design, similar to drawing a structural diagram of a
house.
• The Network Model represents data as interconnected records with predefined relationships.
It allows for many-to-many relationships and uses a graph-like structure. For example, in a
company’s database, employees can work on multiple projects, and each project can have
multiple employees assigned to it. The Network Model connects employee records to project
records through relationship pointers, enabling flexible relationships.
Check the below image:
• The ER Model represents data using entities, attributes, and relationships. Entities are real-world objects,
attributes describe their properties, and relationships depict connections between entities. For instance,
in a university database, entities could be Students and Courses, with attributes like student ID and course
name. Relationships, such as “Student enrolls in Course,” illustrate the associations between entities.
employees, and department heads. These hierarchical links are graphically represented by
the hierarchical model, allowing top-to-bottom or bottom-to-top navigation.
Check the below image:
4. RELATIONAL MODEL:
• The Relational Model organizes data into tables consisting of rows and columns. It create
relationships between tables using primary and foreign keys. For example, in a customer and
orders scenario, customer information is stored in one table, while order details are stored in
another. The Relational Model connects the tables using a shared key, like a customer ID, to
link relevant records.
Relation Model
Order ID Customer ID Order Date Total Amount
Tuples
Primary Key 1001 1 2023-07-01 250.00 (Rows)
1002 2 2023-07-02 150.00
1003 3 2023-07-03 300.00
1004 4 2023-07-04 200.00
What is Database?
A database is like a digital warehouse where data is stored, organized, and managed efficiently.
Database is a physical or digital storage system that implements a specific data model. Database is
the actual implementation of that design, where the data is stored and managed. It acts as a central
hub for information, making it easier to access and analyze data.
Key Components of a Database:
• Data: Data is the raw information stored in a database, such as customer details, product informa-
tion, or financial records. It can be in different formats like text, numbers, dates, or images.
• Tables: Tables are like virtual spreadsheets within a database. They have rows and columns, where
each row represents a specific record or instance, and each column represents a particular piece of
data. For example, a table for customers may have columns like ID, Name, Address, and Contact.
• Queries: Queries are like search commands that allow users to extract specific data from the data-
base. Users can search, filter, and sort data based on the criteria they specify. For example, a query
can be used to find all customers who made a purchase in the last month.
There Are Various Types Of Databases Used In Different Scenarios Based On Their De-
sign And Functionality. Here Are Some Common Types Of Databases:
• Relational Databases (RDBMS): Relational databases separate data into rows and columns and
tables, and they build associations between tables using keys. They are frequently used in business
applications that manipulate data using Structured Query Language (SQL). Oracle, MySQL, and Mic-
rosoft SQL Server are a few examples.
• NoSQL Databases: NoSQL databases are Non-relational databases that offer flexible data models
without using a fixed schema. They can manage enormous amounts of semi- or unstructured data.
MongoDB, Cassandra, and Couchbase are a few examples.
• Object-Oriented Databases: Similar to object-oriented programming, OODBs store data as objects.
They are helpful for programs that deal with intricate interactions and data structures. Examples
are ObjectDB and db4o.
• Hierarchical Databases: Hierarchical databases organize data in a tree-like structure, where each
record has a parent-child relationship. They are suitable for representing hierarchical relationships,
such as organization structures. IMS (Information Management System) is an example of a hierar-
chical database.
• Network Databases: Network databases are similar to hierarchical databases but allow for more
complex relationships. They use a network model where records can have multiple parent and child
records. CODASYL DBMS (Conference on Data Systems Languages) is an example of a network da-
tabase.
• Graph Databases: Graph databases store data in a graph structure with nodes and edges. They are
designed to represent and process relationships between data points efficiently. They are commonly
used for social networks, recommendation engines, and network analysis. Examples include Neo4j
and Amazon Neptune.
• In-Memory Databases: In-memory databases store data primarily in memory, resulting in faster
data access and processing compared to disk-based databases. They are suitable for applications
that require high-speed data operations. Examples include Redis and Apache Ignite.
• Time-Series Databases: Time-series databases are optimized for storing and retrieving time-
stamped data, such as sensor data, financial data, or log files. They provide efficient storage and
retrieval of time-series data for analysis. Examples include InfluxDB and Prometheus.
A DBMS is required to efficiently manage the flow of data within an organization. It handles tasks such
as inserting data into the database and retrieving data from it. The DBMS ensures the consistency and
integrity of the data, as well as the speed at which data can be accessed.
Similarly, an RDBMS is required when we want to manage data in a relational manner, using tables
and relationships. It helps in reducing data duplication and maintaining the integrity of the database.
RDBMS ensures that data is stored in a structured manner, allowing for efficient querying and retrieval.
Differences between DBMS and RDBMS:
DBMS RDBMS
Applications using DBMS save data in files. RDBMS applications store data in a tabular
form.
No relationship between data. Related data stored in the form of table.
WHAT IS NORMALIZATION?
The process of normalization data in a database ensures data integrity by removing duplication. A database
must be split up into various tables, and linkages must be established between them. Different levels of nor-
malization exist, such as 1NF (First Normal Form), 2NF (Second Normal Form), 3NF (Third Normal Form),
and BCNF (Boyce-Codd Normal Form)
Original Table:
2NF Tables:
Table 1: Products
1 Laptop Electronics
2 Smartphone Electronics
Table 2: Orders
3NF Tables:
Table 1: Customers
Table 2: Products
ProductID ProductName
1 Laptop
2 Smartphone
Table 3: Orders
Binary Relationships:
A binary relationship exists when two different relationship are involved. Accordingly, every entity in the
connection has a unique association with one entity in the other entity. For instance, a passport can only
be issued to one individual, and a person is only allowed to have one passport at a time. An illustration of a
one-to-one relationship might be this.
Cardinality:
Cardinality refers to the number of instances of an entity that can be associated with an instance of another
entity. There are four types of cardinality:
One-to-One
Each instance of one entity can only be linked to one instance of the other, and vice versa, in a one-to-one
relationship. It is commonly used to represent one-to-one things in the actual world, such a person and
their passport, and is the strictest kind of relationship.
Example: A person can have only one passport, and a passport can be issued to only one person.
One-to-Many
A one-to-many relationship means that each instance of one entity can be associated with multiple instanc-
es of the other entity, but each instance of the other entity can only be associated with one instance of the
first entity. This is a common type of relationship, and it is often used to represent hierarchical relation-
ships in the real world, such as a parent and their children.
Example: A customer can place multiple orders, but each order can only be placed by one customer.
Many-to-One
An entity in A is associated to no more than one entity in B in this particular cardinality mapping. Or, we
may say that any number (zero or more) of entities or things in A can be connected to a unit or thing in B.
Example: A single surgeon performs many operations in a specific institution. A many-to-one relationship
is one of these relationships.
Many-to-Many
A many-to-many relationship means that each instance of one entity can be associated with multiple in-
stances of the other entity, and each instance of the other entity can also be associated with multiple in-
stances of the first entity. This is the most common type of relationship, and it is often used to represent
relationships where the order of the entities does matter, such as a student and their courses.
Example: A student can take multiple courses, and each course can be taken by multiple students.
INTRODUCTION TO SQL:
SQL (Structured query language) is a computer programming language designed specifically for maintain-
ing and modifying relational databases. It offers statements and commands for creating database struc-
tures. Retrieving and altering data, and carrying out a variety of data operations. Below are other details:
The meaning and object of SQL:
SQL is declarative language used for relational database management, based on its definition and in-
tended use. Users can interact with databases to access, modify, and manage structured data by creating
queries. Regardless of the underlying database management system, SQL offers a consistent and effec-
tive way to work with database.
Pg. No.9 2021-2024
BRAINALYST - SQL SERIES
Why SQL?
Due to its adaptability and efficiency in maintaining relational databases, SQL is frequently used in data
science and analytics. The main justifications for SQL high value are follows:
• core activities of inserting, updating, and deleting data in relational databases are made available to
data professionals via SQL. it gives a simple and effective method for changing data.
• SQL gives customers the ability to get particular data from relational database management sys-
tems. users can provide criteria and conditions to retrieve the desired information by creating sql
queries.
• SQL is useful for expressing the structure of stored data. users can define the structure, data types,
and relationships of database tables as well as add, change, and delete them.
• SQL gives users the ability to handle databases and their tables efficiently. in order to increase the
functionality and automation of database operations, it facilitates the construction of views, stored
procedures, and functions.
• Sql gives users the ability to define and edit data that is kept in a relational database. data con-
straints can be specified by users, preserving the integrity and consistency of the data.
• Data security and permissions: SQL has tools for granting access to and imposing restrictions on
table fields, views, and stored procedures. giving users the proper access rights promotes data se-
curity.
SQL constraints
• Rules for the data in a table can be specified using SQL constraints.
• The kinds of data that can be entered into a table are restricted by constraints. This guarantees
the reliability and accuracy of the data in the table. The action is stopped if there is a violation
between the constraint and the data action.
• Column-level or table-level constraints are both possible. Table level restrictions apply to the
entire table, while column level constraints just affect the specified column.
In SQL, the following restrictions are frequently applied:
1. NOT NULL: A column cannot have a NULL value by using the NOT NULL flag.
2. UNIQUE: A unique value makes sure that each value in a column is distinct.
3. PRIMARY KEY: A NOT NULL and UNIQUE combination. Identifies each table row in a
unique way.
4. FOREIGN KEY: Prevent acts that would break linkages between tables.
5. CHECK - Verifies if the values in a column meet a certain requirement.
6. DEFAULT: If no value is specified, DEFAULT sets a default value for the column.
7. CREATE INDEX - Used to easily create and access data from the database.
Check Constraint:
• The value range that can be entered into a column is restricted by the CHECK constraint.
• Only specific values will be permitted for a column if you define a CHECK constraint on it.
• A table’s CHECK constraint can be used to restrict the values in specific columns based on the
values of other columns in the same row.
• By utilizing operations like GETDATE, the DEFAULT constraint can also be utilized to insert system
data ()
The following list includes the many key types that SQL Server supports:
1. Candidate Key
2. Primary Key
3. Unique Key
4. Alternate Key
5. Composite Key
6. Super Key
7. Foreign Key
1. Candidate Key:
• Candidate key is a table’s primary key that has the potential to be chosen. there may
be several candidate keys in a table, but only one can be chosen to serve as the main
key.
Example: candidate keys are License Number, Employee Id, , and Passport Number.
2. Primary key
• Table primary key was chosen as a candidate key to uniquely identify each record. pri-
mary keys maintain unique values throughout the column and do not permit null val-
ues. Employee Id is the primary key of the Employee table in the example above. SQL
Server, a heap table’s main key automatically builds a clustered index (a table which
does not have a clustered index is known as a heap table). table non-clustered primary
key can also be defined by explicitly specifying the kind of index.
• A table can have only one primary key, in SQL Server, the primary key can be defined
using SQL commands below:
• CRETE TABLE statement (at the time of table creation) – In this case, system defines the
name of primary key
• ALTER TABLE statement (using a primary key constraint) – User defines the name of
the primary key
• Example: Employee_Id is a primary key of Employee table.
3. Unique key
• Similar to a primary key, a unique key prevents duplicate data from being stored in a
column. In comparison to the primary key, it differs in the following ways:
• One null value may be present in the column.
• On heap tables, it by default creates a nonclustered index.
4. Alternate Key
• The alternate key is a potential primary key for the table that has not yet been chosen.
• For instance, other keys include License Number and Passport Number.
5. Composite Key
• Each row in a table is uniquely identified by a composite key, sometimes referred to as
a compound key or concatenated key. A composite key’s individual columns might not
be able to identify a record in a certain way. It may be a candidate key or a primary key.
• Example: To uniquely identify each row in the salary database, Employee Id and Salary
Month Year are merged. Each entry cannot be individually identified by the Employee
Id or Salary Month Year columns alone. The Employee Id and Salary Month Year col-
umns in the Salary database can be used to build a composite primary key.
6. Super Key
• A super key is a group of columns from which the table’s other columns derive their
functional dependence. Each row in a table is given a unique identification by a collec-
tion of columns. Additional columns that are not strictly necessary to identify each row
uniquely may be included in the super key. The minimal super keys, or subset of super
keys, are the primary key and candidate keys.
• In the previous example, the Employee table’s Employee Id column served as a super
key for the Employee Table because it was sufficient to uniquely identify each row of
the table.
• As an illustration, consider the following: “Employee Id,” “Employee Id, Employee
Name,” “Employee Id, Employee Name, Address,” etc.
SQL syntax:
SQL syntax is the set of rules that govern how SQL statements are written. It is a language that is used to
interact with relational databases. SQL statements can be used to create, read, update, and delete data
in a database.
The basic syntax of a SQL statement is:
• Keyword [parameter_1, parameter_2, ...]
The keyword is the name of the SQL statement. The parameters are the values that are passed
to the statement.
Pg. No.13 2021-2024
BRAINALYST - SQL SERIES
For example, the following is a SELECT statement that selects the Name and Department ID
columns from the Students table:
SELECT Name, Department ID
FROM Students;
The keyword is SELECT, and the parameters are Name and Department ID.
SQL Commands
• The set of guidelines governing the writing of SQL statements is known as SQL syntax. The com-
mands CREATE, DROP, INSERT, and others are employed in the SQL language to do the necessary
activities.
• A table receives instructions from SQL statements. It is used to perform some Tasks on the data-
base. Additionally, it is utilized to carry out particular duties, functions, and data inquiries. Table
creation, data addition, table deletion, table modification, and user permission setting are just a
few of the activities that SQL is capable of.
The five main categories into which these SQL commands fall are as follows:
1. Data Definition Language (DDL)
2. Data Query Language (DQL)
3. Data Manipulation Language (DML)
4. Data Control Language (DCL)
5. Transaction Control Language (TCL)
SYNTAX:
CREATE DATABASE database_name
CREATE TABLE table_name (
column1 datatype,...);
DROP: Use the DROP command to remove objects from the database.
SYNTAX:
-- For database
DROP DATABASE database_name;
-- For table
DROP TABLE table_name;
SYNTAX
TRUNCATE: This function is used to eliminate every record from a table, along with any spaces
set aside for the records.
SYNTAX:
SYNTAX
-- rename table
RENAME table_name TO new_table_name;
-- renaming a column
ALTER TABLE table_name
RENAME COLUMN column_name TO new_column_name;
SYNTAX
INSERT INTO table_name (column1, column2,..)
VALUES (value1, value2,..);
SYNTAX:
UPDATE table_name
SET column1 = new_value1, ...
WHERE condition;
• DELETE: Delete records from a database table using the DELETE command.
SYNTAX:
DELETE FROM table_name
WHERE condition;
SYNTAX:
LOCK TABLE table_name IN lock_mode;
SYNTAX:
CALL subprogram_name
REVOKE: This command withdraws the user’s access privileges supplied by using the GRANT
command.
SYNTAX:
REVOKE SELECT, UPDATE ON MY TABLE FROM USER1, USER2;
SQL statements:
Specific queries or operations entered into the database using the SQL language are known as SQL
statements. they are employed to access, modify, or administer data stored in database tables. key-
words, expressions, clauses, and operators make up SQL statements.
SELECT *
All of the columns in the table we are searching will be returned when SELECT is used with an as-
terisk (*).
INPUT:
SELECT *
FROM employees;
OUTPUT:
SELECT DISTINCT
• If there are duplicate records, SELECT DISTINCT will only return one of each; otherwise, it
will provide data that is distinct.
• The code below would only retrieve rows from the Employees table that had unique names.
INPUT:
SELECT DISTINCT(fullname)
FROM employees;
OUTPUT:
SELECT INTO
• The data supplied by SELECT INTO is copy from one table to another.
SELECT * INTO customer_orders
FROM employees;
ALIASES (AS):
• Aliases are frequently used to improve the readability of column names.
• An alias only exists while that query is running.
INPUT:
SELECT FullName as FN
FROM employees;
OUTPUT:
From
FROM identifies the table from which we are obtaining our data.
SELECT *
FROM EMPLOYEES
INPUT:
SELECT *
FROM EMPLOYEES
WHERE EMPID=321
OUTPUT:
NOTE: Single quotes must be used around text values in SQL. However, quotations should not be
used around numeric fields:
AND - INPUT:
OUTPUT:
OR – OPERATOR
INPUT:
SELECT EMPID,
FULLNAME
FROM EMPLOYEES
WHERE EMPID = 121
OR FullName = “Walter White”;
OUTPUT:
NOT OPERATOR
INPUT:
SELECT EMPID, FULLNAME
FROM employees
WHERE NOT EMPID=121
OUTPUT:
OUTPUT:
OUTPUT:
OUTPUT:
OUTPUT:
IS NULL OPERATOR
Testing for empty values is done with the IS NULL operator (NULL values).
To check for non-empty values, use the IS NOT NULL operator (NOT NULL values).
• Example: The SQL statement below lists every employee whose “city” field has a
NULL value:
SELECT FullName, EmpId, city
FROM EMPLOYEES
where city is null;
DELETE
FROM EMPLOYEES
where city =’Toronto’;
LIMIT CLAUSE:
The number of rows to return is specified using the LIMIT clause.
On huge tables with tens of thousands of records, the LIMIT clause is helpful. Performance
may be affected if many records are returned.
• The SELECT TOP clause is not supported by all database management systems.
In contrast to Oracle, which employs FETCH FIRST n ROWS ONLY and ROWNUM,
MySQL supports the LIMIT clause to restrict the number of records that are select-
ed.
INPUT:
SELECT *
FROM EMPLOYEES
LIMIT 2;
OUTPUT:
OUTPUT:
for instance:
SELECT SUM(ORDER_AMOUNT)
FROM CUSTOMER_ORDERS;
OUTPUT:
OUTPUT:
OUTPUT:
OUTPUT:
OUTPUT:
Like Operator:
Look for a specific pattern in a column, use the LIKE operator in a WHERE clause.
Two wildcards are frequently combined with the LIKE operator:
Percent sign (%) denotes a character or many characters. underscore character (_) stands for a
single character.
• Please take note that MS Access substitutes an asterisk (*) for the percent sign (%) and a
question mark (?) for the underscore ( ).
• You can combine the underscore and the % sign!
Operator Discription
WHERE FULLNAME LIKE ‘a%’ Searches for any values beginning with
“a,”
WHERE FULLNAME LIKE ‘%a’ Searches for values ending in “a.”
WHERE FULLNAME LIKE ‘%or%’ identifies any values that contain the word
“or” anywhere.
WHERE FULLNAME LIKE ‘_r%’ in the second position is done by using the
WHERE
WHERE FULLNAME LIKE ‘a_%’ Finds any values that begin with “a” and
have at least two characters in length us-
ing the WHERE
WHERE FULLNAME LIKE ‘a__%’ Search any values that start with “a” and
are at least 3 characters in length.
WHERE FULLNAME LIKE ‘a%o’ Search any values that start with “a” and
ends with “o”.
SELECT *
FROM EMPLOYEES
WHERE FULLNAME LIKE ‘W%’;
OUTPUT:
IN SQL Operator
You can define several values in a WHERE clause by using the IN operator.
The numerous OR conditions are abbreviated as the IN operator.
SYNTAX:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
OR
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
Example: Select all records where city in california and new delhi.
SELECT *
FROM employees
WHERE CITY IN (‘CALIFORNIA’,’NEW DELHI’)
OUTPUT:
BETWEEN OPERATOR:
• The BETWEEN operator chooses values from a predetermined range. The values could be
text, integers, or dates.
• The BETWEEN operator includes both the begin and finish variables.
BETWEEN EXAMPLE: select order amount between 2100 to 2900 from customer orders
SELECT *
FROM customer_orders
WHERE ORDER_AMOUNT BETWEEN 2100 AND 2900;
OUTPUT:
Not between:
Example: select order amount which not in range 2100 to 2900 from customer orders table.
SELECT *
FROM customer_orders
WHERE ORDER_AMOUNT NOT BETWEEN 2100 AND 2900
OUTPUT:
BETWEEN with IN :
Example: select all records where order amount range between 2100 and 2900 and also order
id 1,4,8
SELECT *
FROM customer_orders
WHERE ORDER_AMOUNT NOT BETWEEN 2100 AND 2900
AND ORDER_ID IN(1,4,8)
OUTPUT:
4. Can you use the NOT operator with the BETWEEN operator?
• Yes, you can use the NOT operator to negate the result of the BETWEEN operator. For
example:
SQL joins
Data is kept in several tables that are connected to one another in relational databases like SQL
Server, Oracle, MySQL, and others via a common key value. As a result, it is frequently necessary
to combine data from two or more tables into one results table. The SQL JOIN clause in SQL Server
makes this simple to do. A JOIN clause is used to combine rows from those tables based on a shared
column between two or more of the tables. They allow you to retrieve data from multiple tables in a
single query, based on common data points. Here’s a detailed explanation of SQL joins:
Types of JOINS:
1. Inner join/ Equijoin
2. Left join
3. Right join
4. Full outer join
5. Cross join/ Cartesian Join
6. Self join
7. Natural join
If the criteria is met, the INNER JOIN keyword selects all rows from both tables. By merg-
ing all rows from both tables that meet the requirement—that is, have the same value for
the shared field—this keyword will provide a result set. Inner joins are a useful way to
combine data from two tables where there is a relationship between the two tables.
Insert values:
INSERT INTO t1 (id) VALUES (1), (1), (1), (1), (2), (3), (NULL);
INSERT INTO t2 (id2) VALUES (1), (1), (1), (1), (2), (3), (4), (5),
(NULL);
Table- T1 Table – T2
INPUT:
SELECT * FROM t1
INNER JOIN t2 ON t1.id = t2.id2;
OUTPUT:
2. Right join:
This join gives back all the rows from the table on the right side of the join as well as any
matching rows from the table on the left. The result-set will include null for any rows for
which there is no matching row on the left. RIGHT OUTER JOIN is another name for RIGHT
JOIN. Similar to LEFT JOIN is RIGHT JOIN.
In Simple Language,
• All rows from the right table are returned by the right join.
• The result will exclude any rows that are present in the left table but not the right
table.
Limitations: Complex queries: When using RIGHT JOINs in complex queries in-
volving multiple tables, it can be challenging to maintain clarity and understand
the relationship between tables. Careful consideration of table order and join
conditions is necessary to produce accurate results.
• Code readability: RIGHT JOINs, especially in more complex queries, can
make the SQL code less readable and harder to interpret.
• Check the below image.
Example:
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id2;
OUTPUT:
3. Left join:
Rows that match for the table on the right side of the join are returned along with all of the
rows from the table on the left side of the join. result-set will include null for all rows for
which there is no matching row on the right side. LEFT OUTER JOIN is another name for
LEFT JOIN. Left joins are a useful way to get all the data from one table, even if there is no
matching data in another table.
Benefits of using left joins:
• Used to get a complete overview of a set of data.
• Used to identify customers who have not yet placed an order.
• Used to join tables that have different schemas.
Limitations of using left joins:
• Return a lot of data, which can make it difficult to analyze.
• Inefficient if there are a lot of rows in the right table that do not have a
matching row in the left table.
• Check the below image.
OUTPUT:
6. Self join:
Self join is a type of join operation in SQL that allows you to join a table with itself. It is
useful when you want to compare rows within the same table or establish relationships
between different rows in the table.
SYNTAX:
SELECT columns
FROM table1 AS t1
JOIN table1 AS t2 ON t1.column = t2.column;
harder to read and understand. It is crucial to provide clear aliases and document the
purpose and logic of the self join for easier comprehension and maintenance.
7. Natural Join:
NATURAL JOIN is a type of join operation in Sql that combines tables based on columns
with the same name and data type. it automatically matches columns between tables and
returns only the matching rows. explanation of natural Join and the difference between
NATURAL Join and Cross Join:
SYNATX:
SELECT * FROM t1
NATURAL JOIN t2;
• Readability: Natural Join can enhance code readability as it reflects the inherent
relationship between tables based on column names. it improves query comprehen-
sion and makes the code more intuitive and self-explanatory.
• Lack of flexibility: Natural join provides limited control over the join conditions. It may not
suit complex scenarios where more specific or custom join conditions are required.
• Performance implications: Natural join might impact performance especially when join-
ing large tables or in cases where indexing is not optimized for the matching columns.
• CROSS JOIN: A CROSS JOIN, or Cartesian join, combines every row from the first table
with every row from the second table, resulting in a Cartesian product. It does not con-
sider any column matching or relationships between tables. It generates a large result
set that includes all possible combinations between the joined tables.
• NATURAL JOIN: Natural join combines tables based on columns with the same name
and data type. it automatically matches the columns between tables and returns only
the matching rows. it considers the column names as the join condition, without ex-
plicitly specifying it.
GROUP BY STATEMENT:
In database management systems like SQL, the group by clause is a strong tool for categorizing rows of data
based on one or more columns. It enables data aggregation and summarization, delivering insightful data
and facilitating effective analysis. The COUNT(), MAX(), MIN(), SUM(), and AVG() aggregate functions are
frequently used with the GROUP BY statement to group the result set by one or more columns. Although
this feature has many advantages, it also has some drawbacks and particular applications.
Benefits of Group By:
• Data Summarization: Grouping enormous datasets according to specified criteria enables us to
summarize them. It makes it possible to compute several summary statistics for each group, includ-
ing counts, averages, sums, minimums, maximums, and more. Understanding the broad traits and
patterns in the data is made easier by this summary.
• Simplified Data Analysis: Group by streamlines the analysis process by combining relevant data. It
aids in the discovery of patterns, trends, and connections in the data. A clearer image is created by
grouping data according to pertinent criteria, which also facilitates efficient decision-making.
Restrictions for Group By:
• Data Loss: Only aggregated results are shown when using the Group by clause; the original detailed
information is lost. This could be a drawback if specific data points or individual records must be
scrutinized for more in-depth research. In some circumstances, complex computations or unique
aggregations might be necessary, which the Group by clause might not directly handle.
SYNTAX:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
• Example: sum up price for each city wise from aemf2 table.
SELECT SUM(PRICE), CITY
FROM aemf2
GROUP BY CITY;
OUTPUT:
HAVIND Clause
Where keyword cannot be used with aggregate functions, the HAVING clause was added to SQL.
Utilizing aggregate function-based conditions, HAVING clause in SQL is used to restrict the re-
sults of a query. To filter grouped data, it is frequently combined with the GROUP BY clause. After
grouping and aggregation the HAVING clause apply criteria to the grouped data. In most cases
the HAVING used in conjunction with the GROUP BY clause.
The HAVING Clauses advantages include:
• Grouped data filtering: HAVING clause capacity to filter grouped data depending on con-
ditions is its main advantage. You can use it to specify complicated conditions that incor-
porate aggregate functions and column values to limit the result set to only the groups
that match particular requirements.
• Flexible aggregate filtering: HAVING clause allows for flexible aggregate filtering of data.
it enables you to specify constraints on an aggregate function’s output.
The having clause has some restrictions.
• Order of evaluation: GROUP BY and aggregate procedures are assessed before the HAV-
ING clause. because they are not yet available during the evaluation of the HAVING condi-
tion, column aliases and aggregate functions established in the SELECT clause cannot be
used in the HAVING clause.
• Performance Impact: When working with huge datasets, the HAVING clause may have
an effect on performance.
SYNTAX:
SELECT column1, column2,...
FROM table
GROUP BY column1, column2,...
HAVING condition;
Example: count the person capacity who have grater then 2 with each city from
aemf2 table.
SELECT count(person_capacity),city
FROM aemf2
group by City
having count(person_capacity)>2;
OUTPUT:
clause is used to filter the grouped data after grouping and aggregation.
Q: Can you use the HAVING clause without the GROUP BY clause?
A: No, the HAVING clause is always used in conjunction with the GROUP BY clause. It operates on
the grouped data resulting from the GROUP BY clause.
Q: What happens if you include a column in the SELECT statement that is not in the GROUP
BY clause?
A: In most SQL implementations, including a column in the SELECT statement that is not in the
GROUP BY clause will result in an error. However, some databases allow it with specific config-
uration settings.
Q: Can you have multiple HAVING clauses in a single SQL query?
A: No, a SQL query can have only one HAVING clause. However, you can use multiple conditions
within the HAVING clause using logical operators such as AND and OR.
Q: How is the ORDER BY clause different from the HAVING clause?
A: The ORDER BY clause is used to sort the final result set based on specified columns, while
the HAVING clause is used to filter the grouped data based on conditions involving aggregate
functions.
Q: Is it possible to use aggregate functions in the HAVING clause?
A: Yes, the HAVING clause is specifically designed to work with aggregate functions. It allows you
to apply conditions to the results of aggregate functions.
Q: Can you use the GROUP BY clause without the HAVING clause?
A: Yes, the GROUP BY clause can be used independently to group rows of data without applying
any conditions or filters
SYNTAX:
Limitations:
Performance issues may arise when working with large datasets.
Every line in the subqueries should return results for every row in the outer query or handle
NULL values gracefully.
When to use:
Use ordinary subqueries when you need to compare data across different tables or re-
trieve data for filtering, sorting, or calculations.
Why use:
They help to extract data from one query to be used in another, providing flexibility and
clarity.
Syntax:
Limitations:
If not optimized, ordinary subqueries can impact performance when working with large
datasets.
3. Combined or correlated subquery
Rules:
Correlated subquery refers to a column from the outer query.
It used when data from both the main and subqueries are needed.
When to use:
Combine subqueries are used when you need to access data from both the main query and
subquery.
Why use:
They enable you to perform calculations or filter data based on the relationship between
the main query and the subquery.
Syntax:
Limitations:
Correlated subqueries can be performance-intensive if not optimized.
INTERSECT:
Use: The INTERSECT operator returns rows that appear in both result sets of two or more
SELECT statements.
Limitations:
Same column and data type requirements as UNION.
Generally not supported in all database systems.
When to Use: Use INTERSECT when you want to find common rows between two or more
result sets.
SYNTAX:
Why Use: It helps you identify common data between different sets.
Why Use: It helps you find differences between two result sets.
Operator ALL:
Provides boolean value that when combined with SELECT, WHERE, and HAVING statements returns
TRUE if All of the subquery values satisfy the criteria.
ALL denotes that the operation must be true for each and every value in the range for the condition to
be true.
SYNTAX:
SELECT ALL column_name
FROM table_name
WHERE condition;
My SQL Functions:
There are several built-in functions in MySQL.
Reference includes sophisticated MySQL functions as well as string, numeric, date, and other data types.
String functions:
String data types can be manipulated and operated on using string functions in SQL. They offer a
range of options for extracting, changing, and editing string values. Here is a thorough description of
several typical SQL string functions:
1. CONCAT(str1, str2, ..):
Example:
SELECT CONCAT(‘Hello’, ‘ ‘, ‘World’)
2. LENGTH(str):
Example:
SELECT LENGTH(‘Hello’)
Output: 5 is return
3. UPPER(str):
• String is changed to uppercase.
Example:
SELECT UPPER(“hello”)
Output: “Hello”
4. LOWER(str):
• lowercases string of characters.
• Extracts a substring with a specified length and beginning at a particular location from a
string.
Example:
SELECT SUBSTRING(‘Hello World’, 7, 5)
Output: returns ‘World’.
6. STRAIGHT(str, length):
• Gets a certain amount of characters from a string left side.
Example:
SELECT LEFT(‘Database’, 4)
Output: ‘data’.
7. Wrong(str, length):
• String right side is emptied of a certain amount of characters.
Example,
SELECT RIGHT(“Table,” “3”)
Output: gives “ble.”
8. TRIM([characters FROM] str]):
• Removes certain characters or leading and trailing spaces from a string.
Example:
SELECT TRIM(‘Hello ‘)
Output: is ‘Hello’.
11. REVERSE(str):
• Reverses order of characters in a string.
12. LEFT():
• Returns sub string from the left of given size or characters.
Example:
Select Left(‘MYSQL is’, 5);
Output: MYSQL
13. Right():
• Return sub string from right side of string.
Example:
Select Right(‘MySQL.Com’, 4)
Output: .com
Date functions:
Date and time data types can be processed in a variety of ways SQL date methods. Offer the ability to
work with, extract from, and format date and time data.
1. Current_Date():
• Returns current date.
Example:
Select Current_Date()
SYNTAX:
SELECT department, AVG(salary) AS average_salary
FROM employees
GROUP BY department;
Window functions:
• Calculation scope: Window functions perform calculations on a “window” or a subset of rows within
a result set.
• Ordering: They are typically used with the ORDER BY clause to define the window’s ordering within
the result set.
• Result granularity: Window functions retain the individual rows in the result set but can calculate
values based on the window’s defined scope.
• Usage: They are used to compute values that are related to the current row but involve other rows
in the result set. Examples of window functions include RANK, ROW_NUMBER, LAG, LEAD, and SUM
with the OVER clause.
• Function_name: Window function name you can use, such as ROW_NUMBER, SUM, AVG, etc.
• rguments: Its optional arguments that the window function accept. For example, SUM(column_
name) would calculate the sum of the specified column.
• PARTITION BY: Optional clause that divides the rows into partitions or groups based on one or
more expressions. The window function is applied separately to each partition.
• ORDER BY: Optional clause that specifies the order in which the rows within each partition should
be processed by the window function. It can use one or more columns and can be sorted in ascend-
ing (ASC) or descending (DESC) order.
• ROWS/RANGE frame_clause: Optional clause that defines the window frame or the subset of rows
within each partition over which the window function operates.
1. ROW_NUMBER(): Each row in a window is given a distinct sequential number using the ROW
NUMBER() function.
Example:
SELECT ROW_NUMBER()OVER(ORDER BY PRICE DESC) as row_no, Price
FROM aemf2
OUTPUT:
2. RANK():
• Gives each row in a window a rank, leaving gaps for tied values.
Example:
SELECT RANK()OVER(ORDER BY PRICE DESC) as row_no, Price
FROM aemf2;
3. DENSE_RANK():
• Gives each row in a window a rank, leaving no gaps for tied values.
Example:
SELECT DENSE_RANK()OVER(ORDER BY PRICE DESC) as row_no, Price FROM ;
4. NTILE():
• Divides a window’s rows into a predetermined number of groups or “tiles.”
Example:
SELECT NTILE()OVER(ORDER BY PRICE DESC) as row_no, Price FROM ;
5. LAG():
• Accesses a previous row’s value within a window.
Example:
SELECT PRICE,LAG(PRICE)OVER(ORDER BY DAY DESC) as row_no, Price
FROM aemf2 ;
OUTPUT:
6. LEAD():
• Accesses the value of a subsequent row within a window.
Example:
SELECT PRICE,LEAD(PRICE)OVER(ORDER BY DAY DESC) as row_no, Price
FROM aemf2 ;
OUTPUT:
7. FIRST_Value():
• Access previous row value within window.
Example:
SELECT FIRST_VALUE(PRICE)OVER(ORDER BY DAY DESC) as FIRST_V, Price
FROM aemf2 ;
OUTPUT:
A CTE’s two constituent parts, the anchor member and the recursive member, are defined by the WITH
clause (optional for recursive queries). The anchor member of the CTE serves as its foundation, while the
recursive member builds upon it through iterative processing.
• Improved Readability: CTEs make complex queries easier to understand by breaking them into
logical sections. This improves code readability and maintainability.
• Code Reusability: CTEs can be referenced multiple times within the same query, eliminating the
need to repeat the subquery logic. This improves code efficiency and reduces the chances of errors.
• Recursive Queries: CTEs can be used for recursive queries where a query refers to its own output.
This enables hierarchical and iterative processing, such as retrieving hierarchical data or calculating
running totals.
Simple Example:
Now, let’s Extract the names of employees along with their corresponding department names using
a CTE.
WITH EmployeeDepartments AS (
SELECT E.EmployeeID, E.FirstName, E.LastName, D.DepartmentName
FROM Employees3 AS E
JOIN Departments AS D ON E.DepartmentID = D.DepartmentID)
SELECT EmployeeID, FirstName, LastName, DepartmentName
FROM EmployeeDepartments;
Output:
Explaination:
In this example, we define a CTE as “EmployeeDepartments” using the WITH clause. The CTE selects
the employee details (EmployeeID, FirstName, LastName) and their corresponding department
names by joining the “Employees” and ‘departments’ tables.
we use the CTE within the main query to directly select the desired columns from the CTE, resulting
in a clear and concise syntax.
You want to find all employees reporting to EmployeeID 1.simple recursive CTE to achieve that:
Input:
WITH RecursiveCTE AS (
SELECT EmployeeID, ManagerID
FROM Employees
WHERE EmployeeID = 1 -- Starting with EmployeeID 1
UNION ALL
• We define a recursive CTE named ‘RecursiveCTE.’ we start with the anchor member, which
selects EmployeeID 1 from the employees table.
• Then, we have the recursive member which joins the Employees table with the RecursiveCTE
itself matching the ManagerID of the current row with the EmployeeID of the previous rows.
It continues to add rows until no more matches are found.
• We select the EmployeeID and ManagerID from the RecursiveCTE, giving us all the employ-
ees reporting to EmployeeID 1.
• Recursive CTEs are helpful when dealing with hierarchical data structures like organization-
al charts or family trees.
• When to use:
• Use the CASE statement when you need to transform data conditionally or categorize data into
different groups.
• CASE is particularly useful when you need to create calculated fields in your query results.
• Why use:
• CASE adds a layer of flexibility to your SQL queries by allowing you to make decisions based
on data conditions.
• It can simplify your queries by reducing the need for complex If-Then-Else logic in your appli-
cation code.
• Its a valuable tool for data transformation and reporting.
• SYNTAX:
• Limitations:
• Avoid nesting too many CASE statements within each other, as it can make your SQL hard to
read and maintain.
• CASE is not a suitable replacement for complex control flow that should be handled by appli-
cation code.
• Important Notes:
• The ELSE clause is optional but recommended for handling unexpected or NULL values.
• When CASE is used in the WHERE clause, it filters rows based on the conditions.
• In the SELECT clause, CASE creates a new column in the result set.
• In the ORDER BY clause, it allows conditional sorting of the result set.
• CASE can be used with any data type, and the result must be consistent in data type across all
branches.
Explanation:
Consider a database of students that includes the columns “StudentID,” “FirstName,” “Last-
Name,” and “Grade.” You should put pupils into the following categories depending on their
grades: “Excellent,” “Good,” “Average,” and “Below Average.” This is made possible via the
CASE WHEN statement.
Example:
Let’s say you have the following student data:
You want to categorize students based on their grades. Here is a easy example using the CASE
WHEN statement:
Input:
SELECT
StudentID,
FirstName,
LastName,
Grade,
CASE
WHEN Grade >= 90 THEN ‘Excellent’
WHEN Grade >= 80 AND Grade < 90 THEN ‘Good’
WHEN Grade >= 70 AND Grade < 80 THEN ‘Average’
ELSE ‘Below Average’
END AS GradeCategory
FROM
Students;
• In this example, the grade category for each student is determined using the CASE WHEN
statement within the SELECT statement. This is how it goes:
• The CASE WHEN statement examines each condition individually for each row.
• It labels the GradeCategory column as “Excellent” if the grade is more than or equal to 90.
• The GradeCategory column is given the designation “Good” if the grade falls within the range
of 80 and 89 (inclusive).
• The GradeCategory column is given the label “Average” if the grade falls within the range of
70 and 79 (inclusive).
• It labels the GradeCategory column as “Below Average” for any other grade values.
• The outcome will include the original columns (StudentID, FirstName, LastName, Grade), as
well as GradeCategory, another column.
Output:
StudentID | FirstName | LastName | Grade | GradeCategory
-------------------------------------------------------
1 | John | Doe | 85 | Good
2 | Jane | Smith | 75 | Average
3 | Michael | Johnson | 92 | Excellent
4 | Sarah | Brown | 65 | Below Average
• When doing conditional evaluations and taking different actions in response to particular
circumstances, the CASE WHEN statement is helpful. It aids in classifying or transforming
data according to logical criteria in your SQL queries.
Pivot operator is used to convert the rows into columns example-. revenue is determined for each mix of
product and area using the SUM(revenue) aggregation function. this will have the following effects:
OUTPUT:
Product | Revenue_East | Revenue_West
-------------------------------------
A | 1000 | 1500
B | 2000 | 1200
Unpivoting:
process of unpivoting involves changing columns into rows. when normalising or transforming data to do
analysis or other activities, it is helpful. UNPIVOT operator is employed for this. Here’s an illustration.
Lets take last example: you have a table named ‘sales’ with columns ‘product’, ‘revenue_east,’ and
‘revenue_West.’ each column represents the revenue for a specific product in the respective region.
Product | Revenue_East | Revenue_West
-------------------------------------
A | 1000 | 1500
B | 2000 | 1200
Unpivot the data and display the revenue for each product in different regions in separate rows, you
can use the Unpivot operator:
SELECT Product,
Region,
Revenue
FROM Sales UNPIVOT (
Revenue FOR Region IN (Revenue_East, Revenue_West)
) AS UnpivotTable;
The UNPIVOT operator is used in the example above to convert the columns into rows. Each time a
product and area are combined, a new row is created with the matching revenue. This will have the
following effects:
Output:
Product | Region | Revenue
-------------------------
A | East | 1000
A | West | 1500
B | East | 2000
B | West | 1200
View named employee_details is created that displays the employee ID, name, and department for
employees in the IT department.
See the data in View:
SELECT * FROM employee_details
Stored Procedures:
SQL-prepared code that has been saved as a stored procedure can be used repeatedly. So, if you frequently
develop SQL queries, save them as stored procedures and call them to execute them. Additionally, you may
supply parameters to stored procedures so that they can take action based on the values of the parameters
you pass.
• Pre-compiled SQL statements are saved in the database as stored procedures.
• They enable you to combine several SQL statements into a single, repeatable unit.
• Stored procedures have the ability to process complex operations, receive input parameters, and
return values or result sets.
Triggers in SQL:
When a particular event takes place on a table, a SQL trigger is a database object linked to that table
that automatically executes a group of SQL queries. Within a database, triggers are used to automate
some tasks, ensure data integrity, and enforce business rules. They can be triggered by a variety of ac-
tions, such as inserting, updating, or deleting data from a table, and they let you carry out extra actions
in response to those actions.
How are SQL triggers implemented?
• SQL triggers are associated with a particular table and defined using SQL statements. The
related trigger code is automatically executed when the indicated trigger event (such as an
INSERT, UPDATE, or DELETE) takes place on that table. The trigger code may include SQL
statements that enforce restrictions, modify data in the same or different tables, or do other
actions. Triggers can be set to run before or after the triggering event and are defined to run
inside the transaction scope.