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

CIA- 1.

3-BBA-G

Information System and E - Business

Topic : Use any 5 queries/commands of SQL

Submitted to:
Prof. Swati Raj

School of Business and Management


Christ ( Deemed to be University),Banglore

Submitted by:
Harsh
2220716
3-BBA-G
INDEX

Sr. No. Particulars Page-No.


1 SQL Introduction 3
2 SQL SELECT 4
3 The SQL AND, OR , and NOT Operators 5
4 The SQL DELETE Statements 6
5 The SQL JOIN 7
6 The SQL GROUP BY Statement 8
7 References 9
Structured Query Language

SQL is, at its heart, a domain-specific language for managing and querying structured data. It offers
a standardized method of communicating with databases, allowing you to conduct actions such as
accessing, adding, updating, and removing data. SQL's elegance rests in its simplicity and strength,
making it a must-have tool for anybody working with data.
SQL is the programming language that allows us to interact with and alter data in relational
databases. Understanding SQL opens the door to a world of possibilities in data processing and
decision-making, whether you're a computer enthusiast, a data analyst, or a business professional.

Certainly, here's a quick rundown of SQL's essential components:

• Data Definition Language (DDL): DDL is concerned with database structure. It has
commands for building, modifying, and removing databases, tables, and their components.
DDL also maintains limitations in order to maintain data integrity.

• Data Manipulation Language (DML): DML is used to interface with data in a database. It
contains operations like as INSERT (adding new data), UPDATE (changing existing data),
and DELETE (deleting data).

• Data Query Language (DQL): DQL is a programming language that is used to get particular
data from a database. The most important DQL command is SELECT, which lets you to
define which data to get depending on conditions and criteria.

• Data Control Language (DCL): DCL regulates database permissions and access rights. To
maintain data security, it offers operations such as GRANT (granting permissions) and
REVOKE (removing rights).

Using SQL commands, these components enable users to define, manage, query, and control data
within a relational database.

3
MYSQL Queries and Results

Queries :
1. SQL SELECT
Syntax : SELECT column1, column2, ...
FROM table_name;
Command : SELECT CustomerName, City
FROM Customers ;
Result :

Applications : The SELECT command is one of the most significant and frequently used
commands in MySQL. Data from one or more tables in a database are retrieved using this
method.
• Data Retrieval: The main purpose of SELECT is to retrieve data from one or more tables in
the database. It allows you to fetch specific columns or all columns from a table.
• Filtering Data: You can use the WHERE clause with SELECT to specify conditions that the
retrieved data must meet. This allows you to filter the data based on certain criteria, such as
retrieving records with specific values or within a certain date range
• Subqueries: You may nest SELECT statements inside of other SELECT statements to
construct subqueries, which let you extract more sophisticated data or do computations
depending on interim findings.
In conclusion, the SELECT command in MySQL is a strong tool that allows you to obtain,
filter, and alter data from the database, making it a crucial component of SQL querying and
data analysis.
4
➢ Examples : SELECT * FROM Customers ;
➢ SELECT City
FROM Customers ;
➢ SELECT DISTINCT Country
FROM Customers ;

2. The SQL AND, OR and NOT Operators


Syntax : SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
Command : SELECT * FROM Customers
WHERE Country = ' Germany ' AND City = ' Berlin ' ;
Result :

Applications : Operator: AND


A row must satisfy all of the requirements in order to be included in the result set when using the
AND operator to combine several conditions. When you wish to concurrently get data that satisfies
every requirement, this method is employed.
Operator: OR
Application: A row must satisfy at least one of the requirements in order to be included in the result
set when using the OR operator to combine several conditions. When you wish to obtain data that
satisfies any of the given requirements, you utilize it.
Operator: NOT
Application: To reject a condition, use the NOT operator. When you wish to get data that doesn't fit
certain criteria, you utilize it.
You may establish flexible and potent conditions to filter data based on certain criteria utilizing
these logical operators, enabling you to extract the necessary data from your database.
➢ Examples : SELECT * FROM Customers
WHERE City='Berlin' OR City='München';
➢ SELECT * FROM Customers
WHERE NOT Country='Germany';
5
➢ SELECT * FROM Customers
WHERE Country='Germany' AND (City='Berlin' OR City='München');

3. The SQL DELETE Statement


Syntax : DELETE FROM table_name WHERE condition;
Command : DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste';
Result :

Application : Records from a database table can be deleted using the SQL DELETE command. It is
a vital part of relational database management systems' (RDBMS') data processing.
Cleaning up data : In order to delete redundant, inaccurate, or out-of-date data from a database, the
DELETE statement is frequently employed. This preserves the integrity and correctness of the data.
Correcting Errors: If inaccurate data has been added into a table, it may be removed using the
DELETE command.
Removing Unwanted Data: The DELETE statement may be used to clean up and delete data that
users or apps have inserted as test or temporary but is no longer required.
When using the DELETE statement, especially without the correct WHERE clauses, it's crucial to
take cautious as it may result in accidental data loss. Before doing any DELETE operations in a
production environment, always be sure you have enough backups and are aware of the
consequences.
➢ Examples : DELETE FROM Customers
WHERE Country = 'Norway' ;
➢ DELETE FROM Customers ;
➢ DELETE FROM Customers
WHERE NOT City = ' Norway ' ;

6
4. The SQL JOIN
Syntax : SELECT columns_from_both_tables
FROM table1
JOIN table2
ON table1.column1 = table2.column2
Command : SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
Result :

Application : Rows from two or more tables are combined using the SQL JOIN command based on
a shared column.
Retrieving Related Data: JOIN is generally used to get information from many tables that are
connected by a foreign key or other common columns. As a consequence, you may aggregate
information from many tables into a single result set
Producing Reports: JOINs are frequently used to produce detailed reports that incorporate
information from numerous tables. For instance, you may merge customer information, order
information, and product data in a sales report using JOINs.
Data analysis: JOINs make it possible to examine and contrast data from several tables. For
instance, you might utilize JOINs to examine how customers and their orders have changed over
time.
➢ Examples : SELECT e.employee_name, m.employee_name AS manager_name
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.employee_id;
➢ SELECT orders.order_id, products.product_name
FROM orders
JOIN order_items ON orders.order_id = order_items.order_id
JOIN products ON order_items.product_id = products.product_id;
7
5. The SQL GROUP BY Statement
Syntax : SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Command : SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;
Result :

Application : Using the SQL GROUP BY command, it is possible to create summary rows with the
same values for the given columns, such as "groups" or "categories." When doing computations on
grouped data, it is frequently used with aggregate functions (such as COUNT, SUM, AVG, MAX,
and MIN).
Aggregation : Calculating aggregates on subsets of data is the main objective of the GROUP BY
function. For summarizing and analyzing data, this is helpful. For each product category, you may
compute the total sales, average order value, or maximum product price.
Data Summarization : GROUP BY is used to construct summary reports that give a general
overview of the data. You may produce reports that include summaries of client activity, staff
performance data, or monthly sales totals.
Analysis of Statistics: GROUP BY is crucial while conducting statistical analysis. Data may be
grouped based on numerous characteristics, and various statistical measures, such as standard
deviation, median, or percentile, can be computed.
Top-N Analysis: You may determine the top or bottom values in a dataset by using GROUP BY,
ORDER BY, and LIMIT clauses. This is frequently used to determine the greatest or lowest sales,
the most well-liked items, or the best-performing workers.

8
➢ Examples : SELECT Shippers.ShipperName, COUNT(Orders.OrderID) AS
NumberOfOrders FROM Orders
LEFT JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID
GROUP BY ShipperName;
➢ SELECT YEAR(order_date) AS year, product_category, COUNT(order_id) AS
order_count FROM orders GROUP BY year, product_category;

References :

➢ Syntax, J. S. O. N. (2012). // w3schools. Com. URL: https://www. w3schools.


com/js/js_json_syntax. asp.

You might also like