Professional Documents
Culture Documents
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 can be used to create a database, add new tables, and manage the
For example, users can create a new table and define its columns and data types
The user can then add data to the table using the INSERT command, and retrieve
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
With its powerful commands and functions, it can handle complex tasks
effortlessly.
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
3. Data Query Language (DQL) statements - used to retrieve data from one or
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
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
There are several fundamental components of a DBMS that are essential for its
Data model: A data model defines the structure of the database and determines
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
3. Data manipulation language (DML): DML allows users to insert, update, and
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
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…
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…
This will return all the data from the "first_name" column in the
"customer" table.
1.5 Using clause to sort query output
particular function. Clauses are used to specify the conditions for the retrieval
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
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.
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.
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:
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:
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:
To compare character or string data, you can use the LIKE operator, which
allows you to perform pattern matching.
For example, the following query retrieves all the rows where the name starts
with the letter 'J':
For example, the following query retrieves all the rows where the hire date
is after January 1, 2020:
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.
For example, the following query retrieves all the rows where the
name starts with the letter 'J':
For example, the following query retrieves all the rows where the hire
date is after January 1, 2020:
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':
For example, the following query retrieves all the rows where the
salary is not equal to 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.
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:
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…
If you want to retrieve all the rows where the age is between 30 and 40, you
can use the following SQL query:
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: