1. Home
  2. Explore
  3. Computer Science
  4. SQL Full Course Tutorial

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

SQL Full Course Tutorial

  • 32Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: SQL Fundamentals

What does SQL stand for and what is its primary purpose?

32 questions

  1. 1

    What does SQL stand for and what is its primary purpose?

    Model answer

    SQL stands for Structured Query Language, and it is used to communicate between a user and a database for managing and manipulating data.

    Explanation

    SQL is the standard language for relational database management, allowing operations like querying, updating, and managing data.

  2. 2

    List the four types of SQL commands and give one example keyword for each.

    Model answer

    DQL (Data Query Language) - SELECT; DML (Data Manipulation Language) - INSERT; DDL (Data Definition Language) - CREATE; DCL (Data Control Language) - GRANT (though DCL is not covered in depth).

    Explanation

    SQL commands are categorized into DQL for queries, DML for data manipulation, DDL for schema changes, and DCL for permissions.

  3. 3

    What is the difference between COUNT(*) and COUNT(column_name)?

    Model answer

    COUNT(*) counts all rows in a table including nulls, while COUNT(column_name) counts only non-null values in that column.

    Explanation

    COUNT(*) includes rows with nulls in any column, whereas COUNT(column_name) excludes rows where that specific column is null.

  4. 4

    Explain the purpose of the GROUP BY clause and give an example.

    Model answer

    GROUP BY groups rows that have the same values in specified columns into summary rows, often used with aggregate functions. For example: SELECT job_title, AVG(salary) FROM jobs GROUP BY job_title;

    Explanation

    GROUP BY is used with aggregate functions to create summary statistics for each group.

  5. 5

    What is the difference between WHERE and HAVING?

    Model answer

    WHERE filters rows before aggregation, while HAVING filters groups after aggregation (used with GROUP BY).

    Explanation

    WHERE cannot be used with aggregate functions, but HAVING can filter based on aggregate results.

  6. 6

    Write a query to select the top 5 highest paid jobs using LIMIT.

    Model answer

    SELECT * FROM job_postings ORDER BY salary_year_avg DESC LIMIT 5;

    Explanation

    ORDER BY sorts the results, and LIMIT restricts the number of rows returned.

  7. 7

    What does the DISTINCT keyword do? Provide an example.

    Model answer

    DISTINCT removes duplicate rows from the result set. Example: SELECT DISTINCT job_title_short FROM job_postings;

    Explanation

    DISTINCT ensures unique values are returned for the specified columns.

  8. 8

    Explain the LIKE operator and the wildcards '%' and '_'.

    Model answer

    LIKE is used for pattern matching; '%' matches zero or more characters, '_' matches exactly one character.

    Explanation

    LIKE allows flexible text searching. For example, WHERE job_title LIKE '%Engineer%' finds any job title containing 'Engineer'.

  9. 9

    What is an alias and how is it used?

    Model answer

    An alias renames a column or table for readability using the AS keyword (optional). Example: SELECT salary_year_avg AS avg_salary FROM jobs;

    Explanation

    Aliases make queries easier to read and are often used in joins and aggregations.

  10. 10

    List the order of execution of SQL clauses (from first to last).

    Model answer

    FROM/JOIN, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT.

    Explanation

    Understanding execution order helps in writing correct and efficient queries.

  11. 11

    What is the difference between LEFT JOIN and INNER JOIN?

    Model answer

    LEFT JOIN returns all rows from the left table and matching rows from the right table, with NULLs for non-matches. INNER JOIN returns only rows that have matches in both tables.

    Explanation

    LEFT JOIN preserves all left table rows, while INNER JOIN requires matches in both tables.

  12. 12

    Explain the purpose of a CTE (Common Table Expression) and give its syntax.

    Model answer

    A CTE defines a temporary result set that can be referenced within a query. Syntax: WITH cte_name AS (SELECT ...) SELECT * FROM cte_name;

    Explanation

    CTEs improve readability and allow modular query construction.

  13. 13

    What is the difference between RANK() and DENSE_RANK() window functions?

    Model answer

    RANK() assigns the same rank to tied values but leaves gaps in the ranking sequence. DENSE_RANK() assigns consecutive ranks without gaps.

    Explanation

    For example, if two rows tie for rank 1, RANK() would give the next rank 3, while DENSE_RANK() would give rank 2.

  14. 14

    How does UNION differ from UNION ALL?

    Model answer

    UNION removes duplicate rows from the combined result set, while UNION ALL includes all rows, preserving duplicates.

    Explanation

    UNION automatically deduplicates, which may require sorting; UNION ALL is faster because it does not check for duplicates.

  15. 15

    What is a subquery and where can it be used?

    Model answer

    A subquery is a query nested inside another query, used inside SELECT, FROM, WHERE, or HAVING clauses.

    Explanation

    Subqueries allow dynamic filtering and complex comparisons based on aggregated data.

  16. 16

    Describe the purpose of the MERGE statement and its typical use case.

    Model answer

    MERGE combines INSERT, UPDATE, and DELETE operations into one statement, commonly used for incremental data loading and batch processing.

    Explanation

    MERGE allows synchronizing a target table with a source table based on matching keys.

  17. 17

    What is the difference between DELETE and TRUNCATE?

    Model answer

    DELETE removes rows based on a condition and can be rolled back; TRUNCATE removes all rows from a table and cannot be rolled back easily, but it is faster.

    Explanation

    DELETE is DML, TRUNCATE is DDL. TRUNCATE resets the table structure but is irreversible within a transaction.

  18. 18

    Write a statement to create a new table named 'employees' with columns: id (integer, primary key), name (varchar), and salary (decimal).

    Model answer

    CREATE TABLE employees (id INTEGER PRIMARY KEY, name VARCHAR(100), salary DECIMAL(10,2));

    Explanation

    CREATE TABLE defines the table structure, including column names, data types, and constraints.

  19. 19

    What does the ALTER TABLE command do? Give one example.

    Model answer

    ALTER TABLE modifies an existing table's structure, such as adding a column. Example: ALTER TABLE employees ADD COLUMN department VARCHAR(50);

    Explanation

    ALTER TABLE allows schema evolution without dropping and recreating the table.

  20. 20

    Explain the concept of a foreign key and how it enforces referential integrity.

    Model answer

    A foreign key is a column that references the primary key of another table, ensuring that values in the foreign key column exist in the referenced table.

    Explanation

    Foreign keys prevent orphan records and maintain data consistency across related tables.

  21. 21

    What is a star schema and what are its components?

    Model answer

    A star schema is a data modeling pattern with a central fact table surrounded by dimension tables. Fact tables contain measurable data, dimension tables provide descriptive context.

    Explanation

    Star schemas are optimized for analytical queries and common in data warehouses.

  22. 22

    Define a bridge table and give an example from the course.

    Model answer

    A bridge table resolves a many-to-many relationship between two tables by containing foreign keys from both. Example: skills_job_dim bridges job_postings and skills_dim.

    Explanation

    Bridge tables allow linking multiple skills to multiple job postings without repeating data.

  23. 23

    What are the four attributes that define a data warehouse according to Bill Inmon?

    Model answer

    Subject-oriented, integrated, non-volatile, and time-variant.

    Explanation

    A data warehouse must be organized by subject, combine data from multiple sources, stable (data not overwritten), and store historical data over time.

  24. 24

    How do you create a database in DuckDB?

    Model answer

    CREATE DATABASE database_name; (in MotherDuck or using DuckDB CLI with a file path).

    Explanation

    DuckDB allows creating databases in-memory or as files for persistence.

  25. 25

    What is the purpose of the .gitignore file?

    Model answer

    It tells Git which files or directories to ignore (not track), such as sensitive credentials, large data files, or system files.

    Explanation

    .gitignore prevents accidental commits of unwanted files.

  26. 26

    Explain the difference between git pull and git fetch.

    Model answer

    git fetch downloads changes from the remote repository to your local repository without merging. git pull does a fetch followed by a merge to update your working directory.

    Explanation

    git pull combines fetching and merging; git fetch only updates remote tracking branches.

  27. 27

    What does the CAST function do? Provide an example.

    Model answer

    CAST converts a value from one data type to another. Example: CAST('123' AS INTEGER) converts string '123' to integer 123.

    Explanation

    Casting is necessary for compatibility and to prevent type errors.

  28. 28

    Describe the difference between DATE_TRUNC and EXTRACT functions.

    Model answer

    DATE_TRUNC rounds a date down to the start of a specified precision (e.g., month). EXTRACT pulls a specific part (e.g., year, month) from a date as an integer.

    Explanation

    DATE_TRUNC is useful for grouping data by month/year, while EXTRACT gets individual components.

  29. 29

    What is the purpose of the COALESCE function? Give an example.

    Model answer

    COALESCE returns the first non-null value from a list of expressions. Example: COALESCE(salary, hourly_rate * 2080, 0) returns the first non-null calculated salary.

    Explanation

    COALESCE is used to handle null values by providing fallback values.

  30. 30

    In the course, what was the first project about?

    Model answer

    The first project was an exploratory data analysis (EDA) of the data engineering job market, analyzing top demanded skills, top paying skills, and optimal skills.

    Explanation

    The project used SQL to query the data warehouse and answer business questions.

  31. 31

    What is a data mart and how does it differ from a data warehouse?

    Model answer

    A data mart is a subject-specific subset of a data warehouse, designed for a particular department or use case. It is typically smaller and optimized for specific analyses.

    Explanation

    Data marts are derived from data warehouses and focus on single business areas.

  32. 32

    In the final project, what was the purpose of the master build script?

    Model answer

    The master build script automated the entire pipeline by sequentially executing SQL scripts to create the data warehouse and all data marts from scratch with one command.

    Explanation

    Automation ensures reproducibility and reduces manual effort.

1 of 32
Question 1 of 32

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent