1. Home
  2. Explore
  3. Computer Science
  4. SQL Basics

Study methods

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

SQL Basics

  • 33Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Fundamentals of SQL and Databases

What does SQL stand for?

33 questions

  1. 1

    What does SQL stand for?

    Model answer

    Structured Query Language.

    Explanation

    SQL stands for Structured Query Language, a standard language for managing and querying relational databases.

  2. 2

    What is the primary purpose of SQL?

    Model answer

    To communicate with databases, allowing you to create, read, update, and delete data.

    Explanation

    SQL is used to interact with databases, performing operations like querying data, inserting new records, updating existing data, and deleting records.

  3. 3

    What does the acronym CRUD stand for in the context of databases?

    Model answer

    Create, Read, Update, Delete.

    Explanation

    CRUD represents the four basic operations performed on database records: Create (insert), Read (select), Update (modify), and Delete (remove).

  4. 4

    Define a database in simple terms.

    Model answer

    A database is a structured collection of data that stores large amounts of information, organized similarly to an Excel spreadsheet but more powerful and efficient for managing large datasets.

    Explanation

    Databases are used by websites, companies, and applications to store and retrieve data, often containing multiple tables with related information.

  5. 5

    What is a DBMS? Name two examples.

    Model answer

    A DBMS (Database Management System) is software that manages databases. Examples include MySQL and Microsoft SQL Server.

    Explanation

    DBMS handles data storage, retrieval, and security. Other examples include PostgreSQL and Oracle.

  6. 6

    Is SQL an ISO standard? Why is that important?

    Model answer

    Yes, SQL is an ISO standard. This means that once you learn SQL, you can work with most database systems with relative ease, although there may be minor differences between implementations.

    Explanation

    Standardization ensures that SQL skills are transferable across different database platforms.

  7. 7

    What are the two main types of databases? Describe the key difference.

    Model answer

    Relational (RDBMS) and non-relational (NoSQL). Relational databases store data in related tables using keys, while non-relational databases store unstructured data without fixed relationships.

    Explanation

    Relational databases like MySQL use tables that are connected via primary and foreign keys. Non-relational databases like MongoDB store data in flexible formats like documents or key-value pairs.

  8. 8

    What command is used to install the MySQL server on Ubuntu Linux?

    Model answer

    sudo apt install mysql-server

    Explanation

    After updating the package list with 'sudo apt update', you install MySQL server using 'sudo apt install mysql-server'.

  9. 9

    How do you verify that the MySQL service is running?

    Model answer

    Use the command: sudo systemctl status mysql

    Explanation

    This command checks the status of the MySQL service; you should see 'active (running)' if it is healthy.

  10. 10

    What command do you use to access the MySQL command line as root on a local installation?

    Model answer

    sudo mysql

    Explanation

    On a local installation with no password set, you can simply run 'sudo mysql'. Alternatively, you might specify a username and password with '-u' and '-p'.

  11. 11

    What is the significance of ending each MySQL command with a semicolon?

    Model answer

    It tells MySQL that the command is complete and ready to be executed.

    Explanation

    The semicolon acts as a statement terminator. Without it, MySQL assumes you are continuing the command on the next line.

  12. 12

    Write the command to display all existing databases in MySQL.

    Model answer

    SHOW DATABASES;

    Explanation

    This command lists all databases currently in the MySQL server.

  13. 13

    How do you create a new database named 'nc_coffee'?

    Model answer

    CREATE DATABASE nc_coffee;

    Explanation

    Replace 'nc_coffee' with your desired database name. The command creates a new empty database.

  14. 14

    How do you select and start using a database named 'nc_coffee'?

    Model answer

    USE nc_coffee;

    Explanation

    This command changes the active database to 'nc_coffee', allowing you to run queries on its tables.

  15. 15

    What command shows all tables in the current database?

    Model answer

    SHOW TABLES;

    Explanation

    It lists the tables present in the currently selected database.

  16. 16

    Write the SQL command to create a table named 'coffee' with columns: id (INT), name (VARCHAR(255)), region (VARCHAR(255)), and roast (VARCHAR(255)).

    Model answer

    CREATE TABLE coffee (id INT, name VARCHAR(255), region VARCHAR(255), roast VARCHAR(255));

    Explanation

    Each column is defined by name and data type. Commas separate column definitions. The statement ends with a semicolon.

  17. 17

    What is meant by 'schema' in a database context?

    Model answer

    A schema defines the structure of a database, including the tables, columns, data types, and relationships.

    Explanation

    When you create tables and define columns, you are designing the database schema.

  18. 18

    Write the command to insert a row into the 'coffee' table with values (1, 'Default Route', 'Ethiopia', 'Light').

    Model answer

    INSERT INTO coffee VALUES (1, 'Default Route', 'Ethiopia', 'Light');

    Explanation

    The VALUES list must match the order of columns in the table. Each value corresponds to the respective column.

  19. 19

    How do you retrieve all columns from the 'coffee' table?

    Model answer

    SELECT * FROM coffee;

    Explanation

    The asterisk (*) is a wildcard that selects all columns from the specified table.

  20. 20

    Write a query to select only the 'name' column from the 'coffee' table.

    Model answer

    SELECT name FROM coffee;

    Explanation

    Replace 'name' with any column name to retrieve only that column's data.

  21. 21

    What does the DESCRIBE command do?

    Model answer

    It shows the structure of a table, including column names, data types, and other details.

    Explanation

    For example, 'DESCRIBE coffee;' displays the fields and types for the coffee table.

  22. 22

    Write a query to select all columns from the 'avengers' table where the origin is 'Earth'.

    Model answer

    SELECT * FROM avengers WHERE origin = 'Earth';

    Explanation

    The WHERE clause filters rows based on a condition. String values must be enclosed in single quotes.

  23. 23

    How would you select all avengers whose origin is either 'Earth' or 'Asgard'?

    Model answer

    SELECT * FROM avengers WHERE origin = 'Earth' OR origin = 'Asgard';

    Explanation

    Use the OR logical operator to combine multiple conditions.

  24. 24

    Write a query to select the aliases of all avengers under the age of 30.

    Model answer

    SELECT alias FROM avengers WHERE age < 30;

    Explanation

    Comparison operators like < can be used with numeric columns. 'alias' is assumed to be the column name for the superhero name.

  25. 25

    How do you retrieve all avengers that are not from Earth?

    Model answer

    SELECT * FROM avengers WHERE NOT origin = 'Earth';

    Explanation

    The NOT operator negates the condition, returning rows where the origin is not 'Earth'.

  26. 26

    Write a query to select all avengers ordered by age from youngest to oldest.

    Model answer

    SELECT * FROM avengers ORDER BY age ASC;

    Explanation

    ORDER BY sorts results. ASC is ascending (youngest first); DESC would be descending (oldest first).

  27. 27

    Write the command to delete a row from the 'avengers' table where the first name is 'Jeff'.

    Model answer

    DELETE FROM avengers WHERE first_name = 'Jeff';

    Explanation

    DELETE removes rows that satisfy the WHERE condition. Be cautious as it can delete multiple rows.

  28. 28

    Write the command to update the 'avengers' table and set the last name to NULL for the character with first name 'Groot'.

    Model answer

    UPDATE avengers SET last_name = NULL WHERE first_name = 'Groot';

    Explanation

    UPDATE modifies existing rows. Use SET to assign new values. NULL represents an unknown or missing value.

  29. 29

    How do you add a new column named 'beard' of type BOOLEAN to the 'avengers' table?

    Model answer

    ALTER TABLE avengers ADD beard BOOLEAN;

    Explanation

    ALTER TABLE changes the table structure. The ADD clause adds a new column with the specified data type.

  30. 30

    What is the purpose of a primary key in a relational database?

    Model answer

    A primary key uniquely identifies each row in a table and is used to create relationships with other tables via foreign keys.

    Explanation

    Primary keys ensure that each record is unique. They are often an integer ID column.

  31. 31

    What is a foreign key?

    Model answer

    A foreign key is a column in one table that references the primary key of another table, establishing a link between them.

    Explanation

    Foreign keys enable relational databases to maintain referential integrity and allow operations like joins.

  32. 32

    What is a JOIN in SQL?

    Model answer

    A JOIN combines rows from two or more tables based on a related column, typically a foreign key relationship.

    Explanation

    JOINs allow you to retrieve data from multiple tables in a single query, such as finding which coffee a customer ordered.

  33. 33

    What is a view in SQL?

    Model answer

    A view is a virtual table based on the result set of a SELECT query. It does not store data itself but presents data from one or more tables.

    Explanation

    Views can simplify complex queries and provide a customized perspective on the data.

1 of 33
Question 1 of 33

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent