0% found this document useful (0 votes)
3 views2 pages

SQL Concepts

Data aggregation in SQL involves summarizing data from multiple rows into a single result, which is crucial for insightful analysis. Key functions include COUNT(), SUM(), and AVG() for calculating totals, averages, and counts, while GROUP BY allows for grouping rows for aggregate functions. Additionally, SQL automation through triggers, stored procedures, and scheduled events enhances database management and task efficiency.

Uploaded by

suraiyyaustad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

SQL Concepts

Data aggregation in SQL involves summarizing data from multiple rows into a single result, which is crucial for insightful analysis. Key functions include COUNT(), SUM(), and AVG() for calculating totals, averages, and counts, while GROUP BY allows for grouping rows for aggregate functions. Additionally, SQL automation through triggers, stored procedures, and scheduled events enhances database management and task efficiency.

Uploaded by

suraiyyaustad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Data Aggregation

1. What is data aggregation, and why is it important in SQL?


Data aggregation is the process of summarizing or combining data from multiple rows
into a single result. It is important in SQL because it allows for insightful data
analysis—such as calculating totals, averages, counts, etc.—especially when
analyzing large datasets.

2. Difference between COUNT(), SUM(), and AVG() with examples:


- COUNT(): Returns the number of rows.
Example: SELECT COUNT(*) FROM orders;

- SUM(): Returns the total of a numeric column.


Example: SELECT SUM(order_amount) FROM orders;

- AVG(): Returns the average of a numeric column.


Example: SELECT AVG(order_amount) FROM orders;

3. How does the GROUP BY clause work, and when should you use it?
GROUP BY groups rows that have the same values in specified columns and allows
aggregate functions to be applied to each group.
Example: SELECT customer_id, SUM(order_amount) FROM orders GROUP BY customer_id;

Automation in SQL
1. What are SQL triggers, and how do they help automate tasks?
SQL triggers are special procedures that are automatically executed in response to
specific events on a table (e.g., INSERT, UPDATE, DELETE).
Example: CREATE TRIGGER log_update AFTER UPDATE ON employees FOR EACH ROW INSERT
INTO employee_audit (emp_id, update_time) VALUES (OLD.id, NOW());

2. Stored procedures with a real-life example:


A stored procedure is a saved collection of SQL statements that can be reused and
executed with parameters.
Example:
CREATE PROCEDURE GetEmployeeDetails(IN emp_id INT)
BEGIN
SELECT * FROM employees WHERE id = emp_id;
END;

3. How can scheduled events in SQL improve database management?


Scheduled events allow SQL tasks (e.g., cleanup, backups, summary calculations) to
run at set times without user intervention.
Example:
CREATE EVENT daily_cleanup
ON SCHEDULE EVERY 1 DAY
DO
DELETE FROM logs WHERE log_date < NOW() - INTERVAL 30 DAY;

Table Constraints
1. What are table constraints, and why are they necessary?
Constraints enforce rules on data in tables, ensuring accuracy, integrity, and
consistency.

2. Difference between PRIMARY KEY, UNIQUE, and FOREIGN KEY:


- PRIMARY KEY: Uniquely identifies each row. Only one per table.
- UNIQUE: Ensures all values in a column are different.
- FOREIGN KEY: Links two tables, enforcing referential integrity.

3. Purpose and working of the CHECK constraint:


CHECK ensures that column values meet specific conditions.
Example: age INT CHECK (age >= 18);

You might also like