Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Mauli DBMS Micropro

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Report on SQL Operators.

Brief Description:-
SQL (Structured Query Language) operators are essential components used in
database management systems to perform various operations on data. They
allow users to manipulate and retrieve data from relational databases. Here is a
report on SQL operators:

1. Arithmetic Operators:
● Addition (+): Adds two values.

● Subtraction (-): Subtracts one value from another.

● Multiplication (*): Multiplies two values.

● Division (/): Divides one value by another.

● Modulus (%): Returns the remainder after division.

2. Comparison Operators:

● Equal to (=): Checks if two values are equal.

● Not equal to (!= or <>): Checks if two values are not equal.

● Greater than (>): Checks if one value is greater than another.

● Less than (<): Checks if one value is less than another.

● Greater than or equal to (>=): Checks if one value is greater than or


equal to another.

3. Logical Operators:
● AND: Returns true if all conditions are true.

● OR: Returns true if at least one condition is true.

● NOT: Reverses the logical state of a condition.


4.Assignment Operators:

● =: Assigns a value to a variable.

● +=: Adds a value to a variable and assigns the result.

● -=: Subtracts a value from a variable and assigns the result.

● *=: Multiplies a variable by a value and assigns the result.

● /=: Divides a variable by a value and assigns the result.

5.Aggregate Functions:

● COUNT(): Returns the number of rows or non-null values in a column.

● SUM(): Calculates the sum of values in a column.

● AVG(): Calculates the average of values in a column.

● MIN(): Returns the minimum value in a column.

● MAX(): Returns the maximum value in a column.

6.String Operators:

● Concatenation (+ or ||): Combines two or more strings.

● LIKE: Searches for a specified pattern in a string.

● IN: Checks if a value matches any value in a list.

These operators enable users to perform a wide range of operations on SQL databases,
including data retrieval, filtering, sorting, aggregation, and more. By utilizing these
operators effectively, users can manipulate and extract the desired information from
the database efficiently.

What are SQL operators?


A SQL operator is a special word or character used to perform tasks. These tasks
can be anything from complex comparisons to basic arithmetic operations. Think of
an operator in SQL like the different buttons on a calculator function.
There are six types of SQL operators that we are going to cover: Arithmetic, Bitwise,
Comparison, Compound, Logical and String.
Arithmetic operators
Arithmetic operators are used for mathematical operations on numerical data,
such as adding or subtracting.
+ (Addition)

The + symbol adds two numbers together.

SELECT 10 + 10;

– (Subtraction)
The – symbol subtracts one number from another.

SELECT 10 - 10;

* (Multiplication)
The * symbol multiples two numbers together.

SELECT 10 * 10;

/ (Division)
The / symbol divides one number by another.

SELECT 10 / 10;

% (Remainder/Modulus)
The % symbol (sometimes referred to as Modulus) returns the remainder of one
number divided by another.

SELECT 10 % 10;

Bitwise operators
A bitwise operator performs bit manipulation between two expressions of the integer
data type. Bitwise operators convert the integers into binary bits and then perform the
AND (& symbol), OR (|, ^) or NOT (~) operation on each individual bit, before
finally converting the binary result back into an integer.

Just a quick reminder: a binary number in computing is a number made up of 0s and


1s.
& (Bitwise AND)
The & symbol (Bitwise AND) compares each individual bit in a value with its
corresponding bit in the other value. In the following example, we are using just single
bits. Because the value of @BitOne is different to @BitTwo, a 0 is returned.

> (Greater than)


The > symbol is used to filter results where a column’s value is greater than the
queried value. In the below example, this query will return all customers that have an
age above 20.

SELECT * FROM customers

WHERE age > 20;

!> (Not greater than)


The !> symbol is used to filter results where a column’s value is not greater than the
queried value. In the below example, this query will return all customers that do not
have an age above 20.

SELECT * FROM customers

WHERE age !> 20;

< (Less than)


The < symbol is used to filter results where a column’s value is less than the queried
value. In the below example, this query will return all customers that have an age
below 20.

SELECT * FROM customers

WHERE age < 20;


!< (Not less than)
The !< symbol is used to filter results where a column’s value is not less than the
queried value. In the below example, this query will return all customers that do not
have an age below 20.

SELECT * FROM customers

>= (Greater than or equal to)


The >= symbol is used to filter results where a column’s value is greater than or equal
to the queried value. In the below example, this query will return all customers that
have an age equal to or above 20.

SELECT * FROM customers

WHERE age >= 20;

<= (Less than or equal to)


The <= symbol is used to filter results where a column’s value is less than or equal to
the queried value. In the below example, this query will return all customers that have
an age equal to or below 20.

SELECT * FROM customers

WHERE age <= 20;

<> (Not equal to)


The <> symbol performs the exact same operation as the != symbol and is used to
filter results that do not equal a certain value. You can use either, but <> is the SQL-
92 standard.

SELECT * FROM customers

WHERE age <> 20;


Compound operators
Compound operators perform an operation on a variable and then set the result
of the variable to the result of the operation. Think of it as doing a = a (+,-,*,etc)
b.

+= (Add equals)
The += operator will add a value to the original value and store the result in the
original value. The below example sets a value of 10, then adds 5 to the value and
prints the result (15).

DECLARE @addValue int = 10

SET @addValue += 5

PRINT CAST(@addvalue AS VARCHAR);

This can also be used on strings. The below example will concatenate two strings
together and print “dataquest”.

DECLARE @addString VARCHAR(50) = “data”

SET @addString += “quest”

PRINT @addString;

-= (Subtract equals)
The -= operator will subtract a value from the original value and store the result in the
original value. The below example sets a value of 10, then subtracts 5 from the value
and prints the result (5).

DECLARE @addValue int = 10

SET @addValue -= 5

PRINT CAST(@addvalue AS VARCHAR);


*= (Multiply equals)
The *= operator will multiple a value by the original value and store the result in the
original value. The below example sets a value of 10, then multiplies it by 5 and prints
the result (50).

DECLARE @addValue int = 10

SET @addValue *= 5

PRINT CAST(@addvalue AS VARCHAR);

/= (Divide equals)
The /= operator will divide a value by the original. The below example sets a value of
10, then divides it by 5 and prints the result (2).

DECLARE @addValue int = 10

SET @addValue /= 5

PRINT CAST(@addvalue AS VARCHAR);

%= (Modulo equals)
The %= operator will divide a value by the original value and store the remainder in
the original value. The below example sets a value of 25, then divides by 5 and prints
the result (0).

DECLARE @addValue int = 10

SET @addValue %= 5

PRINT CAST(@addvalue AS VARCHAR);

Logical operators
Logical operators are those that return true or false, such as the AND operator, which
returns true when both expressions are met.
ALL
The ALL operator returns TRUE if all of the subquery values meet the specified
condition. In the below example, we are filtering all users who have an age that is
greater than the highest age of users in London.

SELECT first_name, last_name, age, location

FROM users

WHERE age > ALL (SELECT age FROM users WHERE location = ‘London’);

ANY/SOME
The ANY operator returns TRUE if any of the subquery values meet the specified
condition. In the below example, we are filtering all products which have any record
in the orders table. The SOME operator achieves the same result.

SELECT product_name

FROM products

WHERE product_id > ANY (SELECT product_id FROM orders);

AND
The AND operator returns TRUE if all of the conditions separated by AND are true.
In the below example, we are filtering users that have an age of 20 and a location of
London.

SELECT *

FROM users

WHERE age = 20 AND location = ‘London’;

BETWEEN
The BETWEEN operator filters your query to only return results that fit a specified
range.

SELECT *
EXISTS
The EXISTS operator is used to filter data by looking for the presence of any record in
a subquery.

SELECT name

FROM customers

WHERE EXISTS

(SELECT order FROM ORDERS WHERE customer_id = 1);

IN
The IN operator includes multiple values set into the WHERE clause.

SELECT *

FROM users

WHERE first_name IN (‘Bob’, ‘Fred’, ‘Harry’);

LIKE
The LIKE operator searches for a specified pattern in a column. (For more
information on how/why the % is used here, see the section on the wildcard character
operator).

SELECT *

FROM users

NOT
The NOT operator returns results if the condition or conditions are not true.

SELECT *

FROM users

WHERE first_name NOT IN (‘Bob’, ‘Fred’, ‘Harry’);


OR
The OR operator returns TRUE if any of the conditions separated by OR are true.In
the below example, we are filtering users that have an age of 20 or a location of
London.

SELECT *

FROM users

WHERE age = 20 OR location = ‘London’;

IS NULL
The IS NULL operator is used to filter results with a value of NULL.

SELECT *

FROM users

WHERE age IS NULL;

String operators
String operators are primarily used for string concatenation (combining two or more
strings together) and string pattern matching.

+ (String concatenation)
The + operator can be used to combine two or more strings together. The below
example would output ‘dataquest’.

SELECT ‘data’ + ‘quest’;

+= (String concatenation assignment)


The += is used to combine two or more strings and store the result in the original
variable. The below example sets a variable of ‘data’, then adds ‘quest’ to it, giving
the original variable a value of ‘dataquest’.

DECLARE @strVar VARCHAR(50)

SET @strVar = ‘data’


% (Wildcard)
The % symbol – sometimes referred to as the wildcard character – is used to match
any string of zero or more characters. The wildcard can be used as either a prefix or a
suffix. In the below example, the query would return any user with a first name that
starts with ‘dan’.

SELECT *

FROM users

WHERE first_name LIKE ‘dan%’;

[] (Character(s) matches)
The [] is used to match any character within the specific range or set that is specified
between the square brackets. In the below example, we are searching for any users
that have a first name that begins with a d and a second character that is somewhere in
the range c to r.

SELECT *

FROM users

WHERE first_name LIKE ‘d[c-r]%’’;

[^] (Character(s) not to match)


The [^] is used to match any character that is not within the specific range or set that is
specified between the square brackets. In the below example, we are searching for any
users that have a first name that begins with a d and a second character that is not a.

SELECT *

FROM users

WHERE first_name LIKE ‘d[^a]%’’;


(Wildcard match one character)
The _ symbol – sometimes referred to as the underscore character – is used to match
any single character in a string comparison operation. In the below example, we are
searching for any users that have a first that begins with a d and has a third character
that is n. The second character can be any letter.

SELECT *

FROM users

WHERE first_name LIKE ‘d_n%’;

Actual Resources Used

Sr. no. Name of resource material Specifications Quantity

1 textbook Database management System 1

2 internet Wikipedia

3 PC windows 11 1
Outputs of the Micro-Projects

The outcome of the report on SQL operators is to provide readers with a


comprehensive understanding of SQL operators and their practical applications.
By reading the report, readers should achieve the following outcomes:

1. Knowledge of SQL Basics: Readers will gain a solid understanding of


SQL and its importance in managing relational databases. They will grasp
the fundamental concepts of SQL and its role in data retrieval,
manipulation, and analysis.

2. Familiarity with SQL Operators: Readers will become familiar with


various SQL operators, including arithmetic, comparison, logical,
concatenation, set, join, subquery, and aggregate operators. They will
understand the syntax, functionality, and usage of these operators in SQL
queries.

3. Ability to Retrieve and Manipulate Data: Readers will acquire the skills
to retrieve and manipulate data effectively using SQL operators. They
will understand how to construct SELECT statements with appropriate
operators and clauses to filter, sort, group, and aggregate data.

4. Understanding of Combining and Comparing Data Sets: Readers will


comprehend how to use set operators (UNION, INTERSECT, EXCEPT)
and join operators (INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL
JOIN) to combine and compare data from multiple tables. They will be
able to construct complex queries that retrieve data from related tables.

5. Proficiency in Using Subqueries: Readers will learn how to employ


subqueries within SQL operators to perform complex operations and
achieve more precise data retrieval. They will understand the concept of
nesting queries and be able to leverage subqueries effectively.

6. Data Analysis Skills: Readers will gain the ability to perform data
analysis tasks using SQL operators. They will understand how to use
aggregate operators (COUNT, SUM, AVG, MIN, MAX) to calculate
summary statistics and derive insights from their databases.
Skill Developed / Learning outcomes of this Micro-Project

1. Problem-Solving Skills: Through the micro-project, participants enhance


their problem-solving abilities. They learn to analyze complex problems,
identify underlying issues, explore alternative solutions, and make
informed decisions to overcome obstacles encountered during the project.

2. Project Management: Micro-projects provide an opportunity to develop


project management skills. Participants learn to plan, organize, and
execute a project within a specified timeframe and resources. They gain
experience in setting clear objectives, defining tasks, allocating resources,
monitoring progress, and delivering project outcomes.

3. Collaboration and Teamwork: Micro-projects often require participants to


work collaboratively in teams or with other stakeholders. They learn to
effectively communicate, coordinate tasks, leverage team members’
strengths, and work towards a common goal.
Applications of this Micro-Project
The report on SQL operators can have various applications in different contexts.
Here are some examples of how the report can be applied:

1. Database Management and Development: The report provides valuable


insights into SQL operators, enabling database administrators and
developers to effectively manage and develop databases. They can use
the knowledge gained to write efficient queries, optimize performance,
and manipulate data according to specific requirements.

2. Data Analyst and Data Scientist Roles: Professionals working in data


analysis and data science can benefit from the report by understanding
how SQL operators can be used to retrieve, transform, and analyze data.
They can apply the knowledge to perform complex data analysis tasks
and derive insights from large datasets.

3. Business Intelligence and Reporting: The report can be applied in the


field of business intelligence and reporting, where SQL is commonly
used to extract and analyze data for generating reports and dashboards.
Professionals can leverage the understanding of SQL operators to write
powerful queries that retrieve and summarize data for business reporting
purposes.

4. Software Development: Software developers working with applications


that interact with databases can apply the report’s knowledge to design
and implement efficient data access layers. They can optimize query
performance, ensure data integrity, and improve overall system
performance by leveraging the appropriate SQL operators.

5. Database Training and Education: The report can serve as a valuable


resource for educators and trainers teaching SQL and database-related
courses.

In summary, the report’s applications span various fields, including database


management, data analysis, software development, business intelligence,
education, research, and individual skill development. The knowledge gained
from the report can be practically applied in real-world scenarios where SQL is
used to interact with relational databases.
Conclusion
In conclusion, the report on SQL operators provides a comprehensive
understanding of the various operators available in the SQL programming
language. The report begins by introducing SQL and its importance in
managing relational databases. It covers the basics of SQL operators, including
arithmetic, comparison, logical, and concatenation operators, explaining their
syntax, functionality, and common use cases.

The report delves into data retrieval and manipulation operators, such as
SELECT, WHERE, GROUP BY, HAVING, and ORDER BY, enabling readers
to filter, sort, group, and aggregate data effectively. It also explores set
operators (UNION, INTERSECT, EXCEPT) and join operators (INNER JOIN,
LEFT JOIN, RIGHT JOIN, FULL JOIN) to combine and compare data from
multiple tables.

Furthermore, the report discusses subquery operators, illustrating how


subqueries can be used within other operators to perform complex operations
and achieve precise data retrieval. It covers aggregate operators (COUNT,
SUM, AVG, MIN, MAX) and their role in calculating summary statistics and
performing data analysis.

The report provides practical examples and case studies that demonstrate the
application of SQL operators in real-world scenarios. It emphasizes best
practices and optimization techniques for using SQL operators effectively,
optimizing query performance, and avoiding common pitfalls.

Overall, the report equips readers with the knowledge and skills needed to work
with SQL operators confidently. It serves as a valuable resource for database
administrators, developers, data analysts, and anyone involved in database
management, data analysis, or software development. By understanding SQL
operators and their applications, readers can write efficient SQL queries,
retrieve and manipulate data effectively, perform data analysis tasks, and apply
best practices for optimal database management.

In conclusion, the report emphasizes the importance of SQL operators in


relational databases and highlights their practical significance in various fields.
It encourages readers to explore further, experiment with SQL operators, and
continuously improve their SQL skills to become proficient in working with
databases.

You might also like