CH 11 Imp
CH 11 Imp
In a database system like LibreOffice Base, queries are tools used to retrieve, update, and manipulate
data stored in tables. Queries allow users to perform specific opera ons on data, such as filtering,
sor ng, aggrega ng, or joining data from mul ple tables, without altering the underlying database
structure.
1. Select Queries:
2. Ac on Queries:
o Types include:
3. Parameter Queries:
o Example: Ask the user to enter a city name to filter customers by city.
4. Aggregate Queries:
5. Join Queries:
Queries are essen al for working with databases efficiently and effec vely. They are designed for the
following reasons:
1. Data Retrieval
3. Data Analysis
4. Combining Data
Example: Link Orders and Customers tables to show customer details for each order.
6. Dynamic Interac on
Example: Allow users to search for customers based on dynamic input like city or name.
Example: Generate a query that calculates the total profit for each product category.
Example: Use a query to feed data into a report summarizing monthly sales trends.
Conclusion
Queries in Base are powerful tools that allow users to interact with, analyze, and manipulate data
stored in a database. They are designed to simplify complex tasks, provide insights, and make data-
driven decision-making more efficient. Whether you're retrieving specific informa on, upda ng
records, or summarizing data, queries are indispensable for effec ve database management.
QS- 2 Use of Criterion in a Query [5 or 10 marks]
A criterion in a query refers to the specific condi on or rule used to filter and retrieve only the
desired records from a database. By applying criteria, you can narrow down the data to focus on
relevant informa on that meets the given requirements.
1. Filter Data
Criteria help reduce large datasets by selec ng only the records that match specified
condi ons.
o Example: Fetch sales records from a specific month instead of the en re dataset.
3. Support Decision-Making
Criteria allow users to focus on ac onable data, aiding in analysis and decision-making.
o Example: Iden fy customers who haven’t made a purchase in the last six months.
o Example: Ask the user to input a city name and retrieve all customers from that city.
o Example: City = 'New York' retrieves all records where the city is New York.
o Example: Salary BETWEEN 30000 AND 50000 retrieves salaries in the given range.
3. Pa ern Matching:
o Example: Name LIKE 'A%' retrieves all names star ng with 'A'.
o Compare values using operators like <, >, <=, >=, etc.
o Example: Age > 18 retrieves all records where the age is above 18.
5. Logical Operators:
o Example: (City = 'London') AND (Salary > 40000) retrieves all records mee ng both
condi ons.
o Example: Email IS NOT NULL retrieves all records with an email address.
1. Basic Filtering:
A query with the criterion Product_Price > 100 retrieves all products priced above $100.
3. Date-Based Criteria:
A criterion like Order_Date >= '2025-01-01' retrieves all orders placed on or a er January 1,
2025.
4. Parameterized Query:
A query with City = [Enter City] prompts the user to input a city name dynamically.
3. Dynamic Interac ons: Allow users to customize data retrieval based on their needs.
4. Improved Decision-Making: Provide targeted insights by filtering out irrelevant informa on.
Conclusion
Criteria in queries are essen al for refining data retrieval processes in a database. They enable users
to apply condi ons that filter, sort, and focus on specific subsets of data, making database
interac ons more precise, efficient, and meaningful. By leveraging criteria effec vely, users can
transform raw data into ac onable insights.
QS- 3 What Are Parameterized Queries? [5 or 10 marks]
A parameterized query is a type of query that uses placeholders, also known as parameters, instead
of hard-coded values. These parameters are replaced with actual values at run me, either provided
by the user or programma cally through an applica on. Parameterized queries enhance flexibility,
security, and reusability in database opera ons.
1. Dynamic Input: Accept user input or variable data during execu on.
2. Reusability: The same query can be executed with different parameter values.
3. Security: Prevent SQL injec on a acks by trea ng user input as data, not executable code.
4. Ease of Maintenance: Reduces the need to rewrite queries for different input values.
1. Placeholders/Parameters: Indicate where the run me value will be inserted. These can be
represented as ? or named parameters like :param_name depending on the database.
2. Input Values: Supplied by the user or applica on to replace the placeholders at run me.
3. Execu on: The database engine processes the query by subs tu ng the placeholders with
the provided values.
When executed, the placeholder ? will be replaced with a specific grade value like 10 or 12.
query = "SELECT * FROM Employees WHERE Department = :dept AND Salary > :salary;"
cursor.execute(query, params)
Here, :dept and :salary are replaced with HR and 50000 during execu on.
3. In LibreOffice Base
In LibreOffice Base, a parameterized query might prompt the user for input dynamically. For
example:
SELECT * FROM Orders WHERE Order_Date >= [Enter Start Date];
When executed, the database prompts the user to enter a start date, which replaces the [Enter Start
Date] placeholder.
o Example: If a user inputs ' OR 1=1 --, it will be treated as data, not code.
4. Improved Performance:
o Prepares the query structure once, allowing the database engine to op mize
execu on for mul ple runs with different parameters.
1. Security:
2. Efficiency:
o Reduces query parsing and op miza on overhead for repeated execu ons.
3. Flexibility:
4. Readability:
o Makes code easier to read and maintain by separa ng query logic from data.
1. Learning Curve:
o Parameter syntax varies across database systems (e.g., ? for SQLite, :name for
PostgreSQL).
Conclusion
Parameterized queries are a powerful tool in database management, offering enhanced security,
flexibility, and efficiency. By replacing hard-coded values with parameters, they enable dynamic input
handling while protec ng against SQL injec on a acks. Widely used in modern applica ons,
parameterized queries are a best prac ce for interac ng with databases securely and effec vely.