- Retrieving data: Selecting specific data from tables based on various criteria
- Manipulating data: Inserting, updating, and deleting data within tables
- Managing data: Defining tables, columns, data types, constraints, and user permissions
SQL finds applications in various fields, including:
- Data analysis and reporting
- Web development
- Business intelligence
- Data warehousing
- Scientific computing
2. Explain the different types of SQL statements (e.g., SELECT, INSERT, UPDATE, DELETE).
Answer: SQL involves different statements to interact with databases:
- SELECT: This statement retrieves data from one or more tables based on specific criteria.
- INSERT: This statement adds new data to a table, specifying column values for each new row.
- UPDATE: This statement modifies existing data in a table, based on specific conditions and updating specific columns.
- DELETE: This statement removes rows from a table, based on specific conditions.
3. What are the different data types used in SQL?
Answer: Common data types in SQL include:
- Numeric: Store numeric values (e.g., INT, DECIMAL)
- Character: Store text data (e.g., CHAR, VARCHAR)
- Date and Time: Store date and time information (e.g., DATE, TIME, DATETIME)
- Boolean: Store True/False values (e.g., BOOLEAN)
4. Describe the difference between a primary key and a foreign key.
Answer:
- Primary key: Uniquely identifies each row in a table, ensuring no duplicates exist. Each table typically has only one primary key.
- Foreign key: References the primary key of another table, establishing a relationship between tables. A table can have multiple foreign keys referencing different primary keys in other tables.
5. Explain the concept of normalization in database design.
Answer: Normalization is a process of organizing data in a database to:
- Reduce data redundancy: Minimize storing the same data in multiple places, improving data consistency and storage efficiency.
- Improve data integrity: Reduce the risk of data inconsistencies arising from updates or deletions in one table affecting other tables.
- Enhance data efficiency: Improve query performance by avoiding unnecessary data retrieval.
6. How do you write a query to filter data based on specific conditions (WHERE clause)
Answer: The WHERE clause allows you to filter data within the SELECT statement based on specific conditions. For example:
SELECT * FROM customers WHERE city = 'New York';
SELECT country, COUNT(*) AS total_customers FROM customers GROUP BY country;
- Aggregate functions: Operate on groups of data and return a single summarized value for each group. Examples include SUM, COUNT, AVG, MIN, MAX.
- Scalar functions: Operate on individual values and return a single value for each input value. Examples include UPPER, LOWER, SUBSTRING.
9. What are common table expressions (CTEs) and how are they beneficial?
Answer: Common table expressions (CTEs) are temporary named result sets defined within a single SQL query. They improve:
- Readability: Break down complex queries into smaller, more understandable steps.
- Reusability: Can be referenced multiple times within the same query, avoiding code repetition.
10. Write a query to retrieve employees with a salary above a specific amount.
Answer:
SELECT * FROM employees WHERE salary > [desired_salary_amount];