You are on page 1of 48

LO1.

Write an SQL statement to retrieve and sort data


SQL (Structured Query Language) is a domain-specific language used to manage
and manipulate data in relational databases.

 It is a powerful tool for managing data that is organized into tables and
relationships between them.

 In simple terms, SQL is used to communicate with a database and perform tasks
such as insertion, deletion, and retrieval of data.
Cont…
SQL syntax comprises words, clauses, and commands that allow users to create
and modify tables, insert and retrieve data, and manage the database.

SQL provides various commands and functions such as SELECT, FROM,


WHERE, JOIN, UNION, GROUP BY, ORDER BY, and many more, that enable
users to retrieve data in a straightforward manner.
Cont…

The SQL language also provides a variety of operators, such as arithmetic


operators, comparison operators, and logical operators that are used to perform
calculations and comparisons on data
Cont…

SQL can be used to create a database, add new tables, and manage the

relationships between them.

 For example, users can create a new table and define its columns and data types

(e.g., text, number, date) using CREATE TABLE command.

The user can then add data to the table using the INSERT command, and retrieve

data from the table using the SELECT command.


Cont…

SQL can also be used to update, delete, and modify existing data in the database.

 Users can use UPDATE command to modify existing data, DELETE command

to remove data, and ALTER command to modify the structure of a table.


Cont…

SQL is an efficient way of managing and manipulating data in a database.

With its powerful commands and functions, it can handle complex tasks

effortlessly.

 It is a critical component of data management and is widely used in various

industries such as finance, healthcare, and e-commerce.


Chapter-one
Write an SQL statement to retrieve and sort data

An SQL statement is a command that is used to perform specific operations on a

database.

It is a type of instruction that is executed by the database management system

(DBMS) to manipulate data or carry out various tasks within a database.

 SQL statements are written using the SQL language and follow a specific syntax.
There are several types of SQL statements which
include:

1. Data Definition Language (DDL) statements - used to create and modify

database objects such as tables and indexes.

2. Data Manipulation Language (DML) statements - used to manipulate data

within a database such as INSERT, UPDATE and DELETE.

3. Data Query Language (DQL) statements - used to retrieve data from one or

more tables in a database such as SELECT.


Cont…
Transaction Control Language (TCL) statements - used to manage transactions in a

database, such as COMMIT and ROLLBACK.

SQL statements are written with a combination of keywords, tables, columns, and

conditions.

For instance, a simple SQL statement for selecting data from a table would look

something like this:

SELECT column1, column2, column3 FROM table name WHERE condition;


Cont.…

This statement selects specific columns from a table named "table_name"


based on a specific condition.

SQL statements are commands used to interact with a database, and they are
crucial in managing, manipulating and retrieving data within a database.
Understanding DBMS Fundamental
A database management system (DBMS) is software that is designed to manage

and control access to a database.

There are several fundamental components of a DBMS that are essential for its

proper functioning and management of a database. These include:

Data model: A data model defines the structure of the database and determines

how data is organized, stored and accessed within the database.

 Common data models include hierarchical, relational, and object-oriented data


Cont…

2. Data definition language (DDL): DDL is a set of commands used to create and

modify database objects such as tables and indexes. It specifies the schema or

structure of a database and provides a framework for organizing data.

3. Data manipulation language (DML): DML allows users to insert, update, and

delete data from a database.

Common DML statements include SELECT, INSERT, UPDATE, and DELETE.


Cont…
4. Query language: A query language is a standardized language used to retrieve data

from a database.

SQL (Structured Query Language) is one of the most widely used query languages.

5. Data indexing: Data indexing is a technique used to speed up database queries by

creating indexes on the data fields. Indexes provide a quick and efficient way to

access data, especially when dealing with large volumes of data.


Cont…

 6. Transactions: Transactions are used to ensure data consistency and integrity within a database.

 A transaction consists of a group of related database operations that are executed as a single unit

of work.

 If any one of these operations fails, the entire transaction is rolled back, ensuring the database

remains consistent.
Cont…

7. Database security: Database security is a critical component of a DBMS.

 It involves controlling access to the database and ensuring that only


authorized users have access to sensitive data.

 A security framework, consisting of authentication, authorization, and audit


trails, is used to secure access to the database.
In summary,

A DBMS is an essential tool for managing and controlling access to


a database.

 Understanding the fundamental components of a DBMS is key to


using it effectively to manage data in an organization.
2.1 Identifying database information requirement
Database information requirement refers to the specific information needs of
an organization or user that a database must meet.

It involves understanding what information is required, how it will be used,


and who needs to access it to perform their tasks within an organization.

Database information requirements are necessary to ensure that the database


is designed and structured in a way that meets the needs of the organization.
Cont…
To determine the database information requirement, an organization should
identify the key data elements and information that are needed to support its
business processes.

This can be done by conducting a detailed analysis of the organization's


operations, policies, procedures, and data requirements.

 It is important to involve stakeholders from different departments and levels


of the organization in this process to ensure that all requirements are
identified and prioritized.
Cont…

Once the database information requirements are identified, they should be


documented in a requirements specification document.

 This document should include the data elements, data attributes, data values,
and relationships between data elements that are required.

The requirements specification should also include data access and security
requirements, including who has access to the data and how it will be
protected.
Cont…

A well-defined set of database information requirements is critical


to designing a database that meets the needs of the organization.

It ensures that the database contains the necessary information to


support the organization's operations, and that the information is
structured and organized in a way that is accessible and meaningful
to the users who need it.
1.3 Retrieving all data from a table following work
procedure
To retrieve all data from a table in SQL, you can use the SELECT
statement without any WHERE clause. The syntax is as follows:

SELECT * FROM table_name;


This will return all the columns and rows from the specified table. If
you only want to select certain columns, you can specify them
instead of using the asterisk:

SELECT column1, column2, column3 FROM table_name;


Make sure to replace "table_nam I'lle" with the actual name of the
table you want to retrieve data from.
1.4 Retrieving data from a specific column in a single
table
Retrieve data from single table and column
To retrieve data from a specific column in a single table in SQL, you
can use the SELECT statement and specify the column name. The
syntax is as follows:

SELECT column_name FROM table_name;

Replace "column_name" with the name of the column you want to


retrieve data from and "table_name" with the actual name of the
table.
Cont…
For example, if you have a table named "customer", and you want to
retrieve the data for the "first_name" column only, the SQL statement
would be:

SELECT first_name FROM customer;

This will return all the data from the "first_name" column in the
"customer" table.
1.5 Using clause to sort query output

• In SQL, a clause is a specific section of a SQL statement that performs a

particular function. Clauses are used to specify the conditions for the retrieval

or manipulation of data, as well as to define the structure and parameters of a

query. Some commonly used SQL clauses include:

1. SELECT Clause: This clause is used to retrieve data from one or more

database tables. It allows you to specify the specific columns to retrieve, the

tables to query, and the conditions for retrieval.

Example: SELECT name, age FROM customers WHERE gender = 'Male';


Cont….
This query uses the SELECT clause to retrieve the name and age columns
from the "customers" table, where the gender is 'Male'.

2. WHERE Clause: This clause is used to filter out specific records based on
a specified condition. It is used to select only specific rows that meet your
criteria.

Example: SELECT * FROM employees WHERE department = 'Sales';

This query uses the WHERE clause to filter the employees table and select
all employees whose department is 'Sales'.
Cont…
• 3. JOIN Clause: This clause is used to combine data from two or more tables into a single table.
It is used to relate data between tables when there is a relationship between them.

Example: SELECT customers.name, orders.order_date FROM customers JOIN orders ON


customers.id = orders.customer_id;

This query uses the JOIN clause to join the "customers" and "orders" tables on the
customer_id field, in order to retrieve the name and order_date of customers who have placed
orders.

4. GROUP BY Clause: This clause is used to group data based on one or more columns. It is
used to aggregate data and perform calculations on subsets of data.
Retrieve restricted row by placing specific criteria in
the clause
 To retrieve restricted rows that meet specific criteria, you can use a WHERE clause in your SQL
query.

 The WHERE clause filters the rows returned by the query based on the conditions specified in the
clause.

 For example, suppose you have a table called employees with columns id, name, age, and salary.

 If you want to retrieve the rows where the age is greater than or equal to 30, you can use the
following SQL query:

 SELECT * FROM employees WHERE age >= 30;


Cont…
You can also use other comparison operators such as <, >, <=, >=, and != to
filter rows based on specific criteria.

Additionally, you can use logical operators such as AND, OR, and NOT to
combine multiple conditions in the WHERE clause.

For example, the following query retrieves all the rows where the age is
greater than or equal to 30 and the salary is greater than or equal to 50000:

SELECT * FROM employees WHERE age >= 30 AND salary >= 50000;
Retrieve restricted row by placing specific criteria in
the select statement
In a SQL query, you can use the SELECT statement to specify the columns that you want
to retrieve from a table.

 If you want to restrict the rows returned by the query based on specific criteria, you can
use the CASE statement or a similar conditional statement within the SELECT statement.

The CASE statement allows you to create conditional expressions that can be used to
determine the value of a column.

You can use the CASE statement to apply different criteria to different rows of data, and
then return a result based on those criteria.
Cont…
 For example, suppose you have a table called employees with columns id, name, age, and salary.

 If you want to retrieve a list of employee names and their corresponding salary multiplied by

1.05 if their age is greater than or equal to 30, you can use the following SQL query:

 SELECT name, CASE

WHEN age >= 30 THEN salary * 1.05

ELSE salary

END AS adjusted_salary

FROM employees
Cont..
In this query, the CASE statement is used to apply the criteria that
salary should be multiplied by 1.05 if the employee's age is greater
than or equal to 30
Boolean operator in clause to compare numeric,
character, string, date and time data?
Boolean operators are used to combine conditions in a WHERE clause of a SQL query.

The most commonly used Boolean operators are AND, OR, and NOT.

To compare numeric data using Boolean operators, you can use the
standard comparison operators such as =, <, >, <=, >=, and <>.

For example, the following query retrieves all the rows where the salary is greater
than 50000 and the age is less than 40:

SELECT * FROM employees

WHERE salary > 50000 AND age < 40;


Cont.…

To compare character or string data, you can use the LIKE operator, which
allows you to perform pattern matching.

The % symbol is used as a wildcard character to represent any number of


characters.

For example, the following query retrieves all the rows where the name starts
with the letter 'J':

SELECT * FROM employees WHERE name LIKE 'J%';


To compare date or time data
To compare date or time data, you can use the standard comparison
operators as well.

For example, the following query retrieves all the rows where the hire date
is after January 1, 2020:

SELECT * FROM employees WHERE hire_date > '2020-01-01';

Note that the date format used in the query may vary depending on the
database system you are using.
OR operator
The OR operator is used to combine multiple conditions in a WHERE clause, and it
returns true if any of the conditions are true.

To compare string data using the OR operator, you can use the LIKE operator along with
the % wildcard character.

 For example, suppose you have a table called products with columns id, name,
and category.

If you want to retrieve all the products that belong to the 'Electronics' category or have the
word 'Bluetooth' in their name, you can use the
SELECT * FROM products WHERE category = 'Electronics' OR name LIKE
'%Bluetooth%';
Comparison operators
Comparison operators are used in the WHERE clause of a SQL query to
compare values and return only the rows that meet the specified condition.

To compare numeric data using comparison operators, you can use the
standard comparison operators such as =, <, >, <=, >=, and <>.

For example, the following query retrieves all the rows where the salary is
greater than 50000:
SELECT * FROM employees WHERE salary > 50000;
To compare character
To compare character or string data, you can use the LIKE operator,
which allows you to perform pattern matching.

The % symbol is used as a wildcard character to represent any


number of characters.

For example, the following query retrieves all the rows where the
name starts with the letter 'J':

SELECT * FROM employees WHERE name LIKE 'J%';


To compare date or time data
To compare date or time data, you can use the standard comparison
operators as well.

For example, the following query retrieves all the rows where the hire
date is after January 1, 2020:

SELECT * FROM employees WHERE hire_date > '2020-01-01';

Note that the date format used in the query may vary depending on
the database system you are using.
Using Boolean operator
Boolean operators are used to combine multiple conditions in the WHERE
clause of a SQL query.

The most commonly used Boolean operators are AND, OR, and NOT.

The AND operator is used to combine two or more conditions, and it returns true
only if all the conditions are true.

For example, the following query retrieves all the rows where the salary is greater
than 50000 and the age is less than 40:
 SELECT * FROM employees WHERE salary > 50000 AND age < 40;
The OR operator
The OR operator is used to combine two or more conditions, and it
returns true if any of the conditions are true.

For example, the following query retrieves all the rows where the
department is either 'Sales' or 'Marketing':

SELECT * FROM employees WHERE department = 'Sales' OR de-


partment = 'Marketing';
The NOT operator
The NOT operator is used to negate a condition, and it returns true
only if the condition is false.

For example, the following query retrieves all the rows where the
salary is not equal to 50000:

SELECT * FROM employees WHERE NOT salary = 50000;

Note that you can use parentheses to group conditions and specify the
order of evaluation.
Cont…

For example, the following query retrieves all the rows where the
salary is greater than 50000 and the age is less than 40, or the
department is 'Sales':

SELECT * FROM employees WHERE (salary > 50000 AND age <
40) OR department = 'Sales';
Cont…
In SQL, you can use the WHERE clause to filter the rows returned by a
query based on specific criteria.

The WHERE clause is followed by one or more conditions that must be


satisfied by the rows to be returned.

The basic syntax of the WHERE clause is as follows:

SELECT column1, column2, ... FROM table_name WHERE condition1


AND/OR condition2 AND/OR condition3 ...
Cont.…
For example, suppose you have a table called employees with
columns id, name, age, and salary.

If you want to retrieve all the rows where the age is greater than or
equal to 30, you can use the following SQL query:

SELECT * FROM employees WHERE age >= 30;

This query will return all the rows from the employees table where
the age column is greater than or equal to 30.
• The basic syntax of the BETWEEN operator is as follows:
• Copy
• SELECT column1, column2, ...
• FROM table_name
• WHERE column_name BETWEEN value1 AND value2;
• Here, column_name is the name of the column you want to filter
on, value1 is the lower bound of the range, and value2 is the upper
bound of the range.
Cont…

For example, suppose you have a table called employees with


columns id, name, age, and salary.

If you want to retrieve all the rows where the age is between 30 and 40, you
can use the following SQL query:

SELECT * FROM employees WHERE age BETWEEN 30 AND 40;

This query will return all the rows from the employees table where
the age column is between 30 and 40, inclusive.
Cont.…

You can also use the NOT BETWEEN operator to retrieve the rows
where the value is not within the specified range.

For example, the following query retrieves all the rows where the age
is not between 30 and 40:

SELECT * FROM employees WHERE age NOT BETWEEN 30


AND 40;

You might also like