0% found this document useful (0 votes)
25 views

All You To Know: To Become A Successful Data Professional

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

All You To Know: To Become A Successful Data Professional

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

Brainalyst’s

ALL YOU NEED


TO KNOW SERIES
To Become a Successful Data Professional

SQL For DATA


ANALYTICS
ABOUT BRAINALYST

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

1. Data Analytics and Consulting


Brainalyst offers a suite of data analytics services designed to help organizations harness the power of their
data. Our consulting services include:

• 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.

2. Artificial Intelligence and Machine Learning


We specialize in deploying AI and ML solutions that enhance operational efficiency and drive innovation.
Our offerings include:

• 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.

3. Training and Development


Brainalyst is committed to fostering a culture of continuous learning and professional growth. We provide:

• Workshops and Seminars: Hands-on training sessions on the latest trends and technologies in
data science and AI.

• Online Courses: Comprehensive courses covering fundamental to advanced topics in data


analytics, machine learning, 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.

WHY CHOOSE BRAINALYST?

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.

• Innovation: A commitment to exploring and implementing the latest advancements in technology.

• 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

2. Basics of SQL 10. Troubleshooting and Best Practices


• Introduction to Data Modeling • SQL Errors and Solutions
• Overview of Database Systems • Best Practices for Efficient SQL
• SQL Syntax and Fundamentals
11. Advanced Topics
3. SQL Data Types and Operators • SQL and NoSQL: Differences and When to Use
• Understanding SQL Data Types Each
• Key SQL Operators • Introduction to Stored Procedures and Triggers

4. SQL Commands 12. Appendix


• Data Definition Language (DDL) • SQL Command Reference
• Data Manipulation Language (DML) • Glossary of Terms
• Data Control Language (DCL) • Additional Resources
• Transaction Control Language (TCL)
• Data Query Language (DQL)

5. Working with SQL Functions


• Numeric and String Functions
• Date and Time Functions
• Advanced SQL Functions

6. Advanced Data Querying


• Using Joins in SQL
• Subqueries: Definition and Usage
• Implementing Set Operations
• SQL Views and Indexes

7. SQL Transactions and Security


• Understanding SQL Transactions
• Security Measures in SQL

8. Optimizing SQL Queries


• Techniques for Query Optimization
• Common SQL Performance Issues

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.

The Importance of SQL


SQL is a fundamental technology in the data-driven world, serving as the backbone for database management across count-
less applications and industries. Its versatility allows users to retrieve, insert, update, and manage data efficiently, making it
indispensable for data analysts, software developers, and database administrators. As you progress through this book, you
will gain insights into how SQL operates under the hood, providing you with the skills to optimize database performance and
leverage data in transformative ways.

What This Book Covers


“SQL Basic to Advance” spans a wide array of topics, structured to guide you from basic concepts to the nuances of advanced
SQL techniques:
1. SQL Fundamentals: Starting with the basics, you will learn about SQL syntax, data types, and essential operations.
This foundation will prepare you for more complex topics.
2. Manipulating Data: Delve into the core operations of SQL, including creating, reading, updating, and deleting data.
Learn how to manipulate tables and databases with precision.
3. Advanced Data Querying: Explore sophisticated querying techniques such as joins, subqueries, and set operations,
which are crucial for complex data analysis and reports.
4. SQL Functions and Procedures: Understand built-in SQL functions for data manipulation and custom routines to
automate common tasks, enhancing the functionality and efficiency of your databases.
5. Optimizing SQL Queries: Gain expertise in query optimization to enhance the performance of your SQL queries,
ensuring they run efficiently even on large datasets.
6. Security and Transactions: Learn about the security features of SQL and how to manage transactions to maintain
data integrity and consistency across multiple operations.
7. Practical Applications: Apply your SQL knowledge to real-world scenarios, integrating SQL with other technologies
and understanding its role in the broader landscape of database applications.

For Whom This Book Is Intended


This book is tailored for individuals who aspire to deepen their understanding of database systems through SQL. It is suitable
for:
• Beginners in database management seeking a structured and comprehensive introduction to SQL.
• Intermediate users looking to solidify their understanding and learn advanced techniques.
• Professionals in data-related fields such as data analysis, software development, or database administration who
require an in-depth knowledge of SQL to enhance their job performance.

Navigating This Book


Each chapter in this book is designed to build upon the knowledge of the previous sections, gradually increasing in complex-
ity and detail. Practical examples and exercises are provided throughout to help you apply what you’ve learned in a hands-on
manner.

Embark on Your SQL Journey


As you turn the pages of this book, you will embark on a journey to master SQL, equipped with the knowledge and skills to
tackle any challenges in database management. By the end of this guide, you will not only understand SQL but also be able
to leverage its powerful features to interpret and manage data effectively, opening doors to new opportunities in your career
and projects.

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

Pg. No.1 2021-2024


BRAINALYST - SQL SERIES

with the products they have ordered.

• 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.

DATA ABSTRACTION LEVELS:


Generally, Data modeling includes multiple levels of abstraction, such as:
Conceptual level: more abstract level: It has to do with kinds of data you must keep and their relation-
ships with one another. Diagrams and other visual aids can be used to illustrate this
Example: store data customers, products, and orders. decide that there is a relationship between cus-
tomers and orders, and between products and orders.
Logical level: middle level of abstraction. Have to do with how the data is stored and organized like SQL
or ER diagrams to show this.
Example: store the data in a relational database. create tables for customers, products, and orders, and
define relationships between the tables.
Physical level: The physical level is the most basic or lowest abstraction. It concerns the particulars of
how the data will be kept on disc. Data types, indexes, and other technical information fall under this
category.
Example: You might decide to store the data in a specific database server, and use a specific data type
for each column. You might also decide to create indexes to improve the performance of queries.

2021-2024 Pg. No.2


BRAINALYST - SQL SERIES

Important perspectives of a data model:


1. NETWORK MODEL:

• 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:

2. ENTITY-RELATIONSHIP (ER) MODEL:

• 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.

Check the below Image:


3. HIERARCHICAL MODEL:
• Data is arranged in a tree-like structure with parent-child relationships using the hi-
erarchical model. There is one parent and several children per record. Consider the
organizational hierarchy, where the CEO is at the top and is followed by the managers,

Pg. No.3 2021-2024


BRAINALYST - SQL SERIES

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.

Check the below image.

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.

2021-2024 Pg. No.4


BRAINALYST - SQL SERIES
• Relationships: Relationships define how tables are connected within a database. They establish
associations based on shared data elements. For instance, a customer’s ID in one table can be
linked to their orders in another table. This helps maintain data consistency and enables efficient
data retrieval.

• 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.

Pg. No.5 2021-2024


BRAINALYST - SQL SERIES

Let’s Understand DBMS and RDBMS:


Although they are both software system used to manage databases, DBMS (Database Management System)
and RDBMS (Relational Database Management System) have different qualities.

Why required DBMS?

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.

Why required RDMS?

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.

Normalization is not present. Normalization is present.


Distributed databases are not supported RDBMS supports distributed database.
by DBMS.
It works with small quantity of data. It works with large amount of data.
Security is less More security measures provided.
Low software and hardware necessities Higher software and hardware necessi-
ties.
Examples: XML Window Registry, Forx- Examples: PostgreSQL, MySQL, Oracle,
pro, dbaseIIIplus etc. Microsoft Access, SQL Server etc.

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)

1NF (First Normal Form):


In 1NF, each column in a table contains only atomic values, meaning it cannot be further divided. There
should be no repeating groups or arrays of values within a single column. Each row in the table should
be uniquely identifiable. Here’s an example:

2021-2024 Pg. No.6


BRAINALYST - SQL SERIES

Original Table:

Customer ID Name Phone no.


1 Jia ria 8978847383
2 John Doe 7899748899,
8899278299
3 Smith tie 9877382892
1NF Table:

Customer ID Name Phone no.


1 Jia ria 8978847383
2 John Doe 7899748899
3 Smith tie 9877382892
4 John Doe 8899278299
2NF (Second Normal Form):
In 2NF, the table is already in 1NF, and each non-key column is dependent on the entire primary key. If
there are partial dependencies, those columns should be moved to a separate table. Here’s an example:
Original Table:

Order ID Product ID Product Category Price


Name

1 1 Laptop Electronics 1000


2 2 Smartphone Electronics 800
3 3 Laptop Electronics 900

2NF Tables:
Table 1: Products

Product ID Product Name Category

1 Laptop Electronics
2 Smartphone Electronics

Table 2: Orders

Order ID Product ID Price


1 1 1000
2 2 800
3 1 900

Pg. No.7 2021-2024


BRAINALYST - SQL SERIES

3NF (Third Normal Form):


In 3NF, the table is already in 2NF, and there are no transitive dependencies. Non-key columns should
not depend on other non-key columns. If there are such dependencies, those columns should be moved
to a separate table. Here’s an example:
Original Table:

Customer ID Order ID Product ID Price Customer Name Customer Email

1 1 1 1000 John Doe [email protected]


2 2 2 800 Jane Smith [email protected]
3 3 1 900 John Doe [email protected]

3NF Tables:
Table 1: Customers

CustomerID CustomerName CustomerEmail


1 John Doe [email protected]
2 Jane Smith [email protected]

3 John Doe [email protected]

Table 2: Products

ProductID ProductName

1 Laptop
2 Smartphone

Table 3: Orders

OrderID CustomerID ProductID Price


1 1 1 1000
2 2 2 800
3 3 1 900

BCNF (Boyce-Codd Normal Form):


BCNF is an advanced form of normalization that addresses certain anomalies that can occur in 3NF. It
ensures that there are no non-trivial functional dependencies of non-key attributes on a candidate key.
Achieving BCNF involves decomposing tables further if necessary.

2021-2024 Pg. No.8


BRAINALYST - SQL SERIES

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.

NOT NULL constraint


• A column may by default contain NULL values.
• A column must not accept NULL values according to the NOT NULL constraint.
• This forces a field to always have a value, thus you cannot add a value to this field while adding a
new record or updating an existing record.
• Syntax:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
price int);

2021-2024 Pg. No.10


BRAINALYST - SQL SERIES

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.

Example: Create check constraint:


CREATE TABLE employee (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18));

SQL DEFAULT CONSTRAINT:


• A column’s default value is set using the DEFAULT constraint.
• If no alternative value is supplied, the default value will be appended to all new records.
• Example: Create Default.

CREATE TABLE EMPLOYEE (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT ‘LONDON’);

• By utilizing operations like GETDATE, the DEFAULT constraint can also be utilized to insert system
data ()

Various key types


We’ll talk about keys and the many types that exist in SQL Server. Let’s define keys to get this topic
started.
What is the Key?
In RDBMS systems, keys are fields that take part in the following operations on tables:
• To establish connections between two tables.
• To keep a table’s individuality.
• To maintain accurate and consistent data in the database.
• Possibly speed up data retrieval by enabling indexes on column (s).

Pg. No.11 2021-2024


BRAINALYST - SQL SERIES

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:

2021-2024 Pg. No.12


BRAINALYST - SQL SERIES

• 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)

DDL (Data Definition Language)


• The SQL statements that can be used to specify the database schema make up DDL, or Data Defi-
nition Language. It is used to create and modify the structure of database objects in the database
and only works with descriptions of the database schema. Although data cannot be created,
modified, or deleted with DDL, database structures can. In most cases, a typical user shouldn’t
use these commands; instead, they should use an application to access the database.

2021-2024 Pg. No.14


BRAINALYST - SQL SERIES

DDL commands list:


CREATE: Create the database or its objects using the CREATE (like database, table, index, func-
tion, views, store procedure, and triggers).

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;

ALTER: This is used to change the database’s structure.

SYNTAX

ALTER TABLE table_name


ADD column_name datatype;
ALTER TABLE table_name
DROP COLUMN column_name;
ALTER TABLE table_name
MODIFY column_name datatype;

TRUNCATE: This function is used to eliminate every record from a table, along with any spaces
set aside for the records.
SYNTAX:

TRUNCATE TABLE table_name;

RENAME: This command is used to rename an existing database object.

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;

Pg. No.15 2021-2024


BRAINALYST - SQL SERIES

DQL (Data Query Language)

• DQL is a sublanguage of SQL that is used to query data from a database.


• It is a declarative language, which means that you tell the database what you want, not how to
get it.
• DQL statements are made up of keywords, operators, and values.
• Some common DQL keywords include SELECT, FROM, WHERE, and ORDER BY.
• SYNTAX
select *
from customer_orders
where customer_id = 100;

DML (Data Manipulation Language)


• SQL statements are part of the DML which is used to manipulate data that is present in data-
bases. It is part of the SQL statement in charge of managing database and data access. DML
statements and DCL statements belong together.
DML commands list:
• INSERT: Data inserted into a table using the INSERT command.

SYNTAX
INSERT INTO table_name (column1, column2,..)
VALUES (value1, value2,..);

• UPDATE: table existing data is updated using UPDATE.

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;

• LOCK: Concurrency under table management.

SYNTAX:
LOCK TABLE table_name IN lock_mode;

• CALL: Call a Java or PL/SQL subprogram.

2021-2024 Pg. No.16


BRAINALYST - SQL SERIES

SYNTAX:
CALL subprogram_name

• EXPLAIN PLAN: It outlines the data access route.

DCL (Data control language)


• DCL comprises commands such as GRANT and REVOKE which largely deal with the rights, per-
missions, and other controls of the database system.
List of DCL commands:
GRANT: command gives users access privileges to the database.
SYNTAX:
GRANT SELECT, UPDATE ON MY TABLE TO SOME USER, ANOTHER USER;

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;

TCL (Transaction Control Language)


A group of tasks are combined into a single execution unit using transactions. Each transaction starts
with a particular task and is completed once every activity in the group has been properly completed.
The transaction fails if any of the tasks are unsuccessful. Therefore, there are only two outcomes for a
transaction: success or failure. Here, you may learn more about transactions. As a result, the following
TCL commands are used to manage how a transaction is carried out:
TCL commands list:
• BEGIN: Opens a transaction with BEGIN
• Syntax:
• COMMIT: Completes a Transaction.
• Syntax:
• COMMIT;
• Rollback: When an error occurs, a transaction is rolled back.
• Syntax:
• ROLLBACK;
• SAVEPOINT: Creates a transactional save point.
• Syntax:
• The SAVEPOINT NAME;

SQL query Execution Order

Pg. No.17 2021-2024


BRAINALYST - SQL SERIES

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 STATEMENT: MySQL


• choose data from a database, use the SELECT statement.
• information received is kept in a result table known as the result-set.
For example: in the code below, we’re choosing a row from a table called employees for a
field called name.
INPUT:
SELECT FullName
FROM employees;
OUTPUT:

SELECT *
All of the columns in the table we are searching will be returned when SELECT is used with an as-
terisk (*).

2021-2024 Pg. No.18


BRAINALYST - SQL SERIES

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.

• By using the AS keyword, an alias is produced.


• For instance, we’re renaming the name column in the code below to fullname: AS renames a
column or table with an alias that we can choose.

Pg. No.19 2021-2024


BRAINALYST - SQL SERIES

INPUT:

SELECT FullName as FN
FROM employees;

OUTPUT:

ALIAS FOR TABLES


SELECT o.OrderID, o.OrderDate, c.CustomerName
FROM Customers AS c, Orders AS o
WHERE c.CustomerName=’Around the Horn’ AND c.CustomerID=o.CustomerID;

From
FROM identifies the table from which we are obtaining our data.
SELECT *
FROM EMPLOYEES

SQL WHERE CLAUSE


• Records can be filtered using the WHERE clause.
• It is used to exclusively extract records that meet a certain requirement.
• The WHERE clause is a part of an SQL SELECT statement that tells the database which rows
to return. It uses a variety of operators to compare values in the database to values that you
specify.

INPUT:
SELECT *
FROM EMPLOYEES
WHERE EMPID=321

OUTPUT:

2021-2024 Pg. No.20


BRAINALYST - SQL SERIES

NOTE: Single quotes must be used around text values in SQL. However, quotations should not be
used around numeric fields:

Operators in the WHERE Clause:


Operator Description
= Equal
> Greater than
< Less than
>= Greater than or equal to
<= less than or equal to
<>, != Not equivalent. Note: This operator may
be written as ! In some SQL versions.
between Within a specific range
like Look for patterns
in to designate a column’s potential values in
numerous ways

Where Clause- Tricky Questions:


1. Can you use aggregate functions in the WHERE clause?
No, you cannot use aggregate functions directly in the WHERE clause. Instead, you typically use
them in the HAVING clause to filter the results of aggregate queries.
2. What is the difference between WHERE and HAVING clauses?
The WHERE clause is used to filter rows before they are grouped or aggregated, while the HAV-
ING clause is used to filter grouped or aggregated results. The HAVING clause can use aggregate
functions, unlike the WHERE clause.
3. Can you use multiple conditions in the WHERE clause?
Yes, you can combine multiple conditions using logical operators such as AND and OR. For
example:
WHERE condition1 AND condition2
WHERE condition1 OR condition2

4. What happens if you use a column alias in the WHERE clause?


The WHERE clause is evaluated before the SELECT clause, so you cannot use column aliases
defined in the SELECT clause directly in the WHERE clause. However, you can use the original
column name or wrap the query in a subquery.
• Remember, the WHERE clause is a powerful tool for filtering and retrieving specific data
from a table based on conditions. It allows you to narrow down the result set and perform
complex queries to meet your specific requirements.

Pg. No.21 2021-2024


BRAINALYST - SQL SERIES

And, or, and not operators in SQL:


• Operators like AND, OR, and NOT can be used with the WHERE clause.
• To filter records based on multiple criteria, use the AND , OR operators:
• If every condition that is divided by AND is TRUE, the AND operator displays a record.
• If any of the terms divided by OR is TRUE, the OR operator outputs a record.
• If the condition(s) is/are NOT TRUE, the NOT operator displays a record.

AND - INPUT:

SELECT EMPID, FULLNAME


FROM EMPLOYEES
WHERE EMPID=121 AND FULLNAME=”John Snow”:

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

2021-2024 Pg. No.22


BRAINALYST - SQL SERIES

OUTPUT:

SQL ORDER BY Keyword


• The result set can be sorted in either ascending or descending order using the OR-
DER BY keyword.
• Records are typically sorted using the ORDER BY keyword in ascending order. Use
the DESC keyword to sort the records in descending order.
Example: Ascending Order by
INPUT:
SELECT EMPID, FULLNAME
FROM employees
ORDER BY FullName

OUTPUT:

Example: Descending Order by


INPUT:
SELECT EMPID, FULLNAME
FROM employees
ORDER BY FullName DESC

OUTPUT:

Pg. No.23 2021-2024


BRAINALYST - SQL SERIES

Example of ORDER BY Several Columns


In this example as you can see SQL Statement selected all column from athlete_
event3 table and order by with multiple column.
INPUT:
SELECT *
FROM athlete_events3
ORDER BY Season, City DESC

OUTPUT:

INSERT INTO STATEMENT


To add new records to a table, use the INSERT INTO statement.

INSERT INTO Syntax


There are two methods to format the INSERT INTO statement:
1. Specify the values to be inserted together with the column names.
2. You do not need to provide the column names in the SQL query if you are adding
values to every column of the table. However, make sure the values are arranged in the
same order as the table’s column headings. In this case, the syntax for INSERT INTO
would be as follows:
INPUT:
INSERT INTO EMPLOYEES(EMPID, FULLNAME, MANAGERID, DATEOFJOINING, CITY)
VALUES(333,”JAY RAO”,999,2019-03-20,”LONDON”)

OUTPUT:

Additionally, you can insert data entry to certain columns.

2021-2024 Pg. No.24


BRAINALYST - SQL SERIES

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;

SQL DELETE statement


Existing records in a table can be deleted using the DELETE statement.
• Reminder: Take care while eliminating records from a table! In the DELETE state-
ment, pay attention to the WHERE clause. Which record(s) should be removed is
specified by the WHERE clause. All records in the table will be erased if the WHERE
clause is left off.
Example: Check below , delete statement deleteing city toronto from employees table.

DELETE
FROM EMPLOYEES
where city =’Toronto’;

Output: Toronto city deleted.

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.

• EXAMPLE: from employees table only 2 records display

Pg. No.25 2021-2024


BRAINALYST - SQL SERIES

INPUT:
SELECT *
FROM EMPLOYEES
LIMIT 2;

OUTPUT:

SQL UPDATE Statement


The existing records in a table can be changed using the UPDATE command.
• When updating records in a table, take caution! In the UPDATE statement, pay attention to
the WHERE clause. The record(s) to be modified are specified by the WHERE clause. The
table’s records will all be updated if the WHERE clause is left off!
UPDATE A multiple Of Records
How many records are updated is determined by the WHERE clause.
Note: When updating records, exercise caution. ALL records will be updated if the
WHERE clause is not included!
Example: Update records fullname rana where city is London
UPDATE employees
SET FULLNAME=’RANA’
WHERE CITY=’London’;

OUTPUT:

SQL Aggregation function:


Number of aggregation functions exist in SQL that can be used to conduct calculations on
a collection of rows and return a single value. Here are few aggregation functions that are
frequently used:
SUM()= total of a column or expression using SUM().

2021-2024 Pg. No.26


BRAINALYST - SQL SERIES

for instance:
SELECT SUM(ORDER_AMOUNT)
FROM CUSTOMER_ORDERS;

OUTPUT:

AVG()= average of a column using AVG().


for instance:
SELECT AVG(ORDER_AMOUNT)
FROM CUSTOMER_ORDERS;

OUTPUT:

COUNT() = Returns number of rows in table using COUNT() function.


Example:
SELECT COUNT(ORDER_AMOUNT)
FROM CUSTOMER_ORDERS;

OUTPUT:

MAX()= MAX() function displays highest value in column or expression.


Example:
SELECT MAX(ORDER_AMOUNT)
FROM CUSTOMER_ORDERS;

Pg. No.27 2021-2024


BRAINALYST - SQL SERIES

OUTPUT:

MIN()= lowest value in the column.


SELECT MIN(ORDER_AMOUNT)
FROM CUSTOMER_ORDERS;

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”.

2021-2024 Pg. No.28


BRAINALYST - SQL SERIES

Example: Find fullname starts with w from employees table

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:

Pg. No.29 2021-2024


BRAINALYST - SQL SERIES

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)

2021-2024 Pg. No.30


BRAINALYST - SQL SERIES

OUTPUT:

Between- Tricky Questions:


1. Does the BETWEEN operator include the boundary values?
• Yes, the BETWEEN operator includes the boundary values. It is an inclusive range oper-
ator, so values equal to value1 or value2 will be included in the result.

2. Can the BETWEEN operator be used with non-numeric data types?


• Yes, the BETWEEN operator can be used with non-numeric data types such as dates,
strings, or timestamps. It compares the values based on their inherent order or alpha-
betic sequence.

3. What happens if value1 is greater than value2 in the BETWEEN operator?


• The BETWEEN operator still functions correctly even if value1 is greater than value2. It
will return rows where the column value falls within the specified range, regardless of
the order of value1 and value2.

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:

WHERE column_name NOT BETWEEN value1 AND value2

5. Can you use the BETWEEN operator with NULL values?


• No, the BETWEEN operator cannot be used with NULL values because NULL represents
an unknown value. Instead, you can use the IS NULL or IS NOT NULL operators to check
for NULL values.
6. Can you use the BETWEEN operator with datetime values and timestamps?
• Yes, the BETWEEN operator can be used with datetime values and timestamps to filter
rows within a specific date or time range.
Remember, the BETWEEN operator provides a convenient way to filter rows based on
a range of values. It is widely used for various data types and allows for inclusive range
comparisons. Understanding its behavior and nuances will help you accurately retrieve
the desired data from your database.

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:

Pg. No.31 2021-2024


BRAINALYST - SQL SERIES

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

1. Inner join/ Equijoin:

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.

Here are some of the benefits of using inner joins:

• They can be used to get a more complete picture of a set of data.


• They can be used to identify customers who have placed an order.
• They can be used to join tables that have different schemas.
Here are some of the limitations of using inner joins:
• They can return less data than other types of joins.
• They can be inefficient if there are a lot of rows in the two tables that do not have
matching values.
• The join condition is typically an equality comparison between the related columns
in the two tables.
• You can use table aliases to simplify the syntax and improve readability.
• You can join more than two tables by extending the join operation using additional
INNER JOIN clauses.
• Check the below image.

2021-2024 Pg. No.32


BRAINALYST - SQL SERIES
Example: First create table T1, and T2.
CREATE TABLE t1 (id INT);
CREATE TABLE t2 (id2 INT);

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:

Pg. No.33 2021-2024


BRAINALYST - SQL SERIES

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:

2021-2024 Pg. No.34


BRAINALYST - SQL SERIES

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.

Example: Left join


SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id2;
OUTPUT:

Pg. No.35 2021-2024


BRAINALYST - SQL SERIES

4. Full Outer Join:


Combining the results of both LEFT JOIN and RIGHT JOIN, FULL JOIN produces the re-
sult-set. rows from both tables are all included in the result-set. The result-set will contain
NULL values for the rows where there was no match. Full outer joins are a useful way to
get all the data from two tables, helpful for getting a complete overview of a set of data.
Benefits of using full outer 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 identify orders that have not yet been assigned to a customer.
Limitations of using full outer joins:
• Return a lot of data, which can make it difficult to analyze.
• Inefficient if there are a lot of rows in the two tables that do not have match-
ing values.
Benefits of CROSS Join:
Cartesian Product: CROSS JOIN generates cartesian product of the two ta-
bles involved, combining every row from the first table with every row from
the second table. useful in scenarios where you need to explore all possible
combinations between two sets of data.
Data Expansion: CROSS Join used to create additional rows, especially
when working with dimension tables or reference tables that have a small
number of rows. This expansion can be beneficial for creating test data or
generating comprehensive reports.
Limitations of CROSS Join:
• Result set size: result set of a CROSS JOIN can grow rapidly, especially
when joining large tables or multiple tables. The Cartesian product gener-
ates a number of rows equal to the product of the number of rows in the
joined tables, which can lead to a significant increase in the dataset’s size.
• Performance impact: Due to the potential for a large result set CROSS
JOIN operation can have a performance impact on the database. It can
consume significant resources and take longer to execute compared to
other types of joins.
• Check the below image:

2021-2024 Pg. No.36


BRAINALYST - SQL SERIES
Example:
SELECT * FROM t1
Cross JOIN t2;

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;

Benefits of self joins:


• Comparing related data: self joins allow you to compare and analyze related data
within the same table. Example- you can compare sales data for different time peri-
ods or evaluate hierarchical relationships within organizational data.
• Establishing relationships: self joins enable you to establish relationships be-
tween rows within a table. this is common in scenarios where data has a hierarchi-
cal structure, such as an employee table with a self-referencing manager column.
• Simplifying complex queries: self joins simplify complex queries by enabling you
to consolidate related information in a single result set. this can help in performing
calculations, aggregations, or generating reports based on self-related data.

Limitations of self joins:


• Performance impact: self joins can have a performance impact, especially on large
tables or when the join condition is not optimized. It is important to ensure proper
indexing on the relevant columns for improved performance.
• Code complexity: self joins can make the SQL code more complex and potentially

Pg. No.37 2021-2024


BRAINALYST - SQL SERIES

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.

Tricky questions related to joins:


What is a self join and when would you use it?
• Self join is a join operation where a table is joined with itself. It is useful when you
want to compare rows within the same table or establish relationships between dif-
ferent rows in the table, such as hierarchical data or comparing related records.
Can you perform a Join operation without specifying the join condition?
• No, specifying the join condition is essential for performing a join operation in SQL.
it defines the relationship between the tables being joined and determines how the
rows are matched.
How does LEFT Join differ from Right Join?
• LEFT Join returns all rows from the left table and the matching rows from the right
table. Right join on the other hand, returns all rows from the right table and the
matching rows from the left table.
What is the result of joining a table with itself using a CROSS JOIN?
• Joining a table with itself using a CROSS Join also known as a cartesian join, results
in the cartesian product of the table. it combines every row from the table with every
other row resulting in a potentially large result set.
Can you JOIN more than two tables in a single SQL query?
• Yes it is possible to join more than two tables in a single SQL query. this is often re-
quired when you need to combine data from multiple tables to retrieve the desired
information.
How can you simulate a FULL OUTER JOIN in a database that does not support it?
• Databases that do not support a FULL Outer Join, a Full Outer Join can be simulated
by combining a Left Join and a Right Join using the UNION operator.
What is the difference between a natural join and an equijoin?
• Natural join is a join operation that automatically matches columns with the same
name and data type between the joined tables. An equijoin on the other hand is a
join operation that explicitly specifies the equality condition between columns in the
joined tables.
Can you use the WHERE clause to perform a Join operation?
• While it is possible to use the WHERE clause to filter rows in a join operation, it is
generally recommended to use the join condition in the ON clause for specifying the
relationship between the tables being joined.
How can you exclude rows that match in a JOIN operation?
• Exclude rows that match in a JOIN operation, you can use an OUTER JOIN (Left JOIN
or right Join) and check for Null values in the columns of the non-matching table.
rows with Null values indicate that they exist in one table but not in the other.

2021-2024 Pg. No.38


BRAINALYST - SQL SERIES

Can you join tables with different data types in SQL?


• Yes, it is possible to join tables with different data types in SQL as long as the join con-
dition is based on compatible data types. SQL will automatically perform necessary
data type conversions if possible.
How can you optimize the performance of a JOIN operation?
• Optimize the performance of a JOIN operation, you can ensure that the join columns
are properly indexed, use appropriate join types (e.g., Inner Join instead of Cross
Join), and apply relevant filtering conditions to reduce the result set size.
Is it possible to Join tables based on non-matching columns?
• Yes, it is possible to join tables based on non-matching columns by using conditions
other than equality in the JOIN clause, such as using greater than or less than oper-
ators.
What are the implications of using a Cartesian product (CROSS JOIN)?
• The Cartesian product can result in a large result set, especially if the joined tables
have many rows. It can consume significant resources, impact query performance,
and lead to unintended data duplication if not used carefully.
Can you JOIN tables that have different column names?
• Yes, it is possible to join tables that have different column names. In such cases, you
can specify the column names explicitly in the join condition using aliases or by using
the ON clause to specify the relationship between the columns with different names.

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;

Benefits of NATURAL Join:


• Simplicity: Natural join simplifies the join operation by automatically matching col-
umns with the same name and data type between the joined tables. It eliminates the
need to explicitly specify the join conditions reducing the complexity and potential
for errors in the query.

• 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.

Limitations of NATURAL Join:


• Ambiguous column names: joined tables contain columns with the same name but differ-
ent meanings, using Natural Join can lead to confusion or produce incorrect results. it relies
column names and data types, disregarding any semantic differences.

Pg. No.39 2021-2024


BRAINALYST - SQL SERIES

• 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.

Difference between CROSS JOIN and NATURAL JOIN:

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

2021-2024 Pg. No.40


BRAINALYST - SQL SERIES

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;

Pg. No.41 2021-2024


BRAINALYST - SQL SERIES

OUTPUT:

Difference between Group by and having:


Purpose:
GROUP BY: The GROUP BY clause is used to group rows together based on one or more col-
umns. It creates distinct groups of data based on the specified columns.
HAVING: The HAVING clause is used to filter the grouped data after the grouping and aggre-
gation have taken place. It applies conditions to the result of aggregate functions.
Usage:
GROUP BY: The GROUP BY clause is used in conjunction with aggregate functions to perform
calculations and summarizations on the grouped data.
HAVING: The HAVING clause is used to filter the grouped data based on conditions involv-
ing aggregate functions. It narrows down the result set to only those groups that satisfy the
specified conditions.
Placement:
GROUP BY: The GROUP BY clause is placed after the FROM and WHERE clauses but before the
ORDER BY clause (if used) in a SQL query.
HAVING: The HAVING clause is placed after the GROUP BY clause in a SQL query. It follows
the GROUP BY clause and precedes the ORDER BY clause (if used).
Evaluation:
GROUP BY: The GROUP BY clause operates on the original rows of data and creates groups
based on the specified columns. It does not filter or remove any rows from the original data-
set.
HAVING: The HAVING clause operates on the grouped data after the GROUP BY and aggrega-
tion steps. It applies conditions to the aggregated results and filters out groups that do not
satisfy the specified conditions.
Tricky Interview Questions with Answers:
Q: What is the difference between the WHERE clause and the HAVING clause?
A: The WHERE clause is used to filter rows before grouping and aggregation, while the HAVING

2021-2024 Pg. No.42


BRAINALYST - SQL SERIES

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.

2. Ordinary subquery (single-row subquery):


Rules:
Ordinary subquery is a standalone query embedded within another query.
It can return multiple rows but must be used with operators like In, =, <, etc.

Pg. No.43 2021-2024


BRAINALYST - SQL SERIES

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.

2021-2024 Pg. No.44


BRAINALYST - SQL SERIES

UNION AND UNION ALL:


To concatenate the result-set of two or more SELECT statements, use the UNION operator.
• Within UNION, all SELECT statements must have an identical number of columns.
• Similar data types must also be present in the columns.
• Every SELECT statement’s columns must be in the same order.
• Syntax:
SELECT column_name FROM table1
UNION
SELECT column_name FROM table2;

Pg. No.45 2021-2024


BRAINALYST - SQL SERIES

UNION ALL Syntax


• By default, the UNION operator only chooses distinct values. To allow duplicate values, use
UNION ALL:
SELECT column_name FROM table1
UNION ALL
SELECT column_name FROM table2;

UNION AND UNION ALL KEY DIFFERENCES:


Duplicate Rows:
UNION: The UNION operator removes duplicate rows from the final result set. It compares all col-
umns in the result sets and eliminates duplicates.
UNION ALL: The UNION ALL operator does not remove duplicate rows. It includes all rows from
each SELECT statement, even if there are duplicates.
Performance:
UNION: The UNION operator implicitly performs a sort operation to remove duplicates. This addi-
tional sorting process can impact performance, especially when dealing with large result sets.
UNION ALL: The UNION ALL operator does not perform any sorting or elimination of duplicates,
making it generally faster than UNION.
Result Set Size:
UNION: The size of the result set produced by the UNION operator may be smaller than the sum of
the individual result sets due to the removal of duplicate rows.
UNION ALL: The size of the result set produced by the UNION ALL operator is equal to the sum of
the individual result sets, including duplicate rows.

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:

2021-2024 Pg. No.46


BRAINALYST - SQL SERIES

Why Use: It helps you identify common data between different sets.

EXCEPT (MINUS IN SOME DATABASE SYSTEMS):


Use: EXCEPT operator returns rows that are present in the first result set but not in the sec-
ond result set.
Limitations:
same column and data type requirements as Union.
Not supported in all database systems.
When to Use: use Except when you want to find rows that are unique to the first result
set.
Syntax:

Why Use: It helps you find differences between two result sets.

Pg. No.47 2021-2024


BRAINALYST - SQL SERIES

SQL ANY AND ALL OPERATORS:


You can compare a single column value to a variety of other values using the ANY and ALL operators.
The ANY Operator in SQL
Using the ANY operator provides a Boolean value in the form of TRUE if ANY of the values returned by the
subquery satisfy the condition ANY denotes that the condition will hold true if the operation holds for ANY
of the values in the range. It can be used with comparison operators such as =, >, <, >=, <=, <> (not equal).
Syntax:
SELECT column_name
FROM table_name
WHERE column_name operator ANY
(SELECT column_name
FROM table_name
WHERE condition);

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.

2021-2024 Pg. No.48


BRAINALYST - SQL SERIES

SYNTAX:
SELECT ALL column_name
FROM table_name
WHERE condition;

Differences between ANY and ALL:


Comparison logic:
ANY: Returns true if the comparison is true for at least one value in the set.
ALL: Returns true if the comparison is true for all values in the set.

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, ..):

• Combines one or more strings into one.

Example:
SELECT CONCAT(‘Hello’, ‘ ‘, ‘World’)

Output: returns ‘Hello world’.

2. LENGTH(str):

• Returns length of a string, or the number of characters.

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.

Pg. No.49 2021-2024


BRAINALYST - SQL SERIES

Example: result of SELECT LOWER(‘WORLD’)


SELECT LOWER(‘WORLD’)
Output: is “world”.
5. SUBSTRING(str, start, length):

• 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’.

9. REPLACE(str, replacement value, search value):


• replaces substring with a new substring wherever it appears in the string.
Example:
SELECT REPLACE(‘Hello, World!’, ‘World’, ‘SQL’)
Output: hello, SQL
10. STRING(str, search str):
• Returns location of substring first occurrence within a string.
Example:
SELECT INSTR(‘Hello World’, ‘World’)
Output: 7

2021-2024 Pg. No.50


BRAINALYST - SQL SERIES

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()

Output: return current date.


2. Current_Time():
• Return current time.
Example:
Select Current_Time()
3. Current_Timestamp():
• Return both the current date and time.
Example:
Select Current_Timestamp()
4. Dateadd(datepart, interval, date):
• Add specific interval of time to a date.
Example:
Select Dateadd(Month, 3, ‘2023-01-01’)
‘2022-04-01’.

5. datediff(datepart, start_date, end_date):


• Difference between two dates
Pg. No.51 2021-2024
BRAINALYST - SQL SERIES

Window Function- Advance:


SQL window functions are analytical operations on a group of rows known as a “window” or “frame” within
the result set. They enable the execution of calculations and aggregations on a per-row basis while taking
the window’s context into account. A thorough explanation of numerous window functions is provided
below, along with a sample code: but lets first understand what is difference between aggregation function
and window function.
In SQL, sets of rows in a table can be calculated using both aggregate functions and window functions. The
methods they use to organise and interpret the data vary, though.
Aggregate functions:
• Calculation scope: Aggregate functions operate on a set of rows and return a single value for the
entire set.
• Grouping: They are typically used with the GROUP BY clause to partition the data into groups and
calculate a result for each group.
• Result granularity: Aggregate functions collapse multiple rows into a single result. For example,
SUM, AVG, COUNT, MAX, and MIN are aggregate functions.
• Usage: They are used to obtain summary statistics or perform calculations such as calculating the
total sales per category, average salary per department, or counting the number of orders per cus-
tomer.

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.

How window function works:


OVER clause, which specifies the window’s borders and ordering, is frequently combined with window
functions in programming.
SYNTAX:
function_name([arguments]) OVER (
[PARTITION BY partition_expression]
[ORDER BY order_expression [ASC|DESC]]
[ROWS/RANGE frame_clause]

2021-2024 Pg. No.52


BRAINALYST - SQL SERIES

• 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.”

Pg. No.53 2021-2024


BRAINALYST - SQL SERIES

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:

2021-2024 Pg. No.54


BRAINALYST - SQL SERIES

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:

SELECT Sales, PERCENT_RANK() OVER (ORDER BY Sales) AS PercentileRank


FROM SalesData;

CTE(Common Table Expression) IN SQL:


Known in SQL as a Common Table Expression (CTE), a temporary named result set may be referred to with-
in a query. Having the ability to build a subquery and utilize it more than once within a single query might
help make complex queries easier to comprehend and maintain. A detailed explanation of CTE is provided
below, along with an example:

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.

Benefits of using CTE:

• 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.

Pg. No.55 2021-2024


BRAINALYST - SQL SERIES

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.

Recursive common table expression (CTE):


• SQL technique called a recursive common table expression (CTE) enables you to query hierarchical
data or data with recursive relationships. It aids in the resolution of issues where each row de-
pends on the outcomes of earlier rows returned by the same query brief explanation:
Consider table of employees where the columns ‘EmployeeID’ and ‘ManagerID’ indicate which
employees report to which managers. You may locate every employee in the reporting chain begin-
ning with a single employee using a recursive CTE.
Example: Suppose you have this employee data.
EmployeeID | ManagerID
----------------------
1 | NULL
2 |1
3 |1
4 |2
5 |2

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

2021-2024 Pg. No.56


BRAINALYST - SQL SERIES

UNION ALL

SELECT E.EmployeeID, E.ManagerID


FROM Employees AS E
INNER JOIN RecursiveCTE AS R
ON E.ManagerID = R.EmployeeID
)
SELECT EmployeeID, ManagerID
FROM RecursiveCTE;
OUTPUT:
EmployeeID | FirstName | LastName | Salary | SalaryRange
-------------------------------------------------------
1 | John | Doe | 50000 | Low
2 | Jane | Smith | 60000 | Medium
3 | Michael | Johnson | 75000 | Medium
4 | Sarah | Brown | 90000 | High

• 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.

Case when –conditional :


SQL CASE WHEN statement is a conditional expression that enables you to take various actions in response
to various situations. it resembles making choices when writing SQL queries.
• Rules:
• CASE statement allows you to perform conditional logic in SQL queries.
• You can use it in the Select, Where, Having, and Order By clauses.
• It can return a single value or multiple values.
• CASE is followed by one or more WHEN clauses and an optional Else clause.

• 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.

Pg. No.57 2021-2024


BRAINALYST - SQL SERIES

• 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:

StudentID | FirstName | LastName | Grade


----------------------------------------
1 | John | Doe | 85
2 | Jane | Smith | 75
3 | Michael | Johnson | 92
4 | Sarah | Brown | 65

You want to categorize students based on their grades. Here is a easy example using the CASE
WHEN statement:

2021-2024 Pg. No.58


BRAINALYST - SQL SERIES

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.

Pivoting and unpivoting techniques:


Pivoting: Pivoting: pivoting and unpivoting techniques, SQL can change the format of data from rows-wise
to column-wise or from column-wisw to row-wise(unpivoting). Detailed explanation:
Turing rows into column is known as pivoting, when you wich to rolate or transform data to generate sum-
mary view it can be helpful. Pivot operator is frequently employees. Example-
Suppose you have a table named ‘sale’ with columns ‘product’,’region’ and ‘revenue’. The table captures the
revenue generated by each product in different regions.

Pg. No.59 2021-2024


BRAINALYST - SQL SERIES

Product | Region | Revenue-------------------------


A | East | 1000
A | West | 1500
B | East | 2000
B | West | 1200
Use pivot operator to pivot the data and display the revenue for each product in separate columns based
on the region:
SELECT Product,
[East] AS Revenue_East,
[West] AS Revenue_West
FROM Sales PIVOT (
SUM(Revenue) FOR Region IN ([East], [West])
) AS PivotTable;

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:

2021-2024 Pg. No.60


BRAINALYST - SQL SERIES

Output:
Product | Region | Revenue
-------------------------
A | East | 1000
A | West | 1500
B | East | 2000
B | West | 1200

What is View in SQL:


• In SQL, views are a subset of virtual tables. The rows and columns of a view are identical to those in
a database’s actual table. By choosing fields from one or more database tables, we can build a view.
A view may include all table rows or only certain rows according to a set of criteria. This post will
teach us how to add, remove, and update Views.
• Example: Create View
• SYNTAX:
CREATE VIEW employee_details AS
SELECT employee_id, employee_name, department
FROM employees
WHERE department = ‘IT’;

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

Create VIEW fro multiple tables:


CREATE VIEW MarksView AS
SELECT StudentDetails.NAME, StudentDetails.ADDRESS, StudentMarks.MARKS
FROM StudentDetails, StudentMarks
WHERE StudentDetails.NAME = StudentMarks.NAME;

If you want to see all view tables:


SYNTAX:
use “database_name”;
show full tables where table_type like “%VIEW”

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.

Pg. No.61 2021-2024


BRAINALYST - SQL SERIES

Lets Create Stored Procedure:


SYNTAX:
CREATE PROCEDURE procedure_name
(parameter1 data_type, parameter2 data_type, …)
AS
BEGIN
— SQL statements to be executed
END

Explanation of the Parameter


• The parameters are the most crucial component. Values are passed to the Procedure using pa-
rameters. There are various types of parameters, including the following:
• BEGIN: This is what actually executes, or we could say it is a section that can be executed.
• END: The code will run up until this point.
Execute the procedure
EXEC procedure_name parameter1_value, parameter2_value,

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.

2021-2024 Pg. No.62

You might also like