Today was all about Aggregation Functions and a quick revision of Constraints in SQL.
What I Learned Today
Aggregation Functions
-
COUNT()
→ Counts the number of rows. -
SUM()
→ Adds up the values in a column. -
AVG()
→ Finds the average of numeric values. -
MIN()
→ Gets the smallest value. -
MAX()
→ Gets the largest value.
Example:
SELECT
COUNT(*) AS total_records,
SUM(salary) AS total_salary,
AVG(salary) AS avg_salary,
MIN(salary) AS min_salary,
MAX(salary) AS max_salary
FROM employees;
SQL Constraints (Revision)
- Primary Key → Uniquely identifies each record in a table.
- Unique Key → Ensures all values in a column are different.
- Foreign Key → Links one table to another.
- References → Used while defining foreign keys.
- Default → Assigns a default value if none is provided.
- NULL → Represents missing or undefined values.
Example:
CREATE TABLE students (
student_id INT PRIMARY KEY,
email VARCHAR(100) UNIQUE,
course_id INT,
admission_date DATE DEFAULT CURRENT_DATE,
FOREIGN KEY (course_id) REFERENCES courses(course_id)
);
Key Takeaway
Aggregation functions help summarize data effectively, while constraints ensure data integrity. Both are essential for building reliable databases.
Top comments (0)