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

Study methods

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

SQL Basics for Beginners

Speaker 1Speaker 2

SQL Basics for Beginners

11:17
0:00-11:17
  1. Simon

    Welcome to our beginner’s guide to SQL. We’ll build from the ground up: what data and databases are, how SQL lets us work with them, the key table and relationship concepts, and then some basic queries you can try in MySQL.

  2. Anna

    Let’s start with data. Data is information about something—facts such as a customer’s age, income, city, or buying habits. When we gather and interpret those facts, we can find useful patterns and make better decisions.

  3. Simon

    That matters to businesses because they handle a lot of information. Understanding customers, for example, may help a business make decisions about its products or services and improve its efficiency.

  4. Anna

    But before we can analyze data, we need somewhere to keep it. A database is an organized place to store data, and it gives us a way to manage, update, retrieve, and analyze that information.

  5. Simon

    A database can contain multiple tables, each holding a particular set of related information. Once data is stored, we can query it to find what we need instead of searching through everything by hand.

  6. Anna

    That brings us to SQL. You’ll hear it pronounced either “S-Q-L” or “sequel.” It’s a query language used to communicate with databases—to retrieve data and to perform actions such as inserting, updating, or deleting it.

  7. Simon

    Imagine a customers table and a request for every customer’s name. A query such as `SELECT name FROM customer;` asks the database for that column. The database processes the request and returns the matching names.

  8. Anna

    So the basic pattern is: write a query, send it to the database, and receive a result. SQL is useful because it lets us ask targeted questions of large amounts of data, and it is used with relational databases and in many business intelligence tools.

  9. Simon

    To understand those queries, we need to understand tables. A table has columns and rows. Columns describe the attributes we store—such as name, age, and city—while each row is one record, such as the details for one customer.

  10. Anna

    You may also hear an individual piece of information called a field or a value. For instance, a customer’s city is a column, and “Delhi” in one customer’s row is a value in that column.

  11. Simon

    Columns have data types, which indicate what kind of values they hold. An ID or age might use an integer type, while a name or city might use a character or text type. Those choices help define the structure of the table.

  12. Anna

    A column can also be required or allowed to be empty. `NOT NULL` means a value must be supplied in that column; without that restriction, the column may allow a `NULL`, meaning the value is missing or unknown.

  13. Simon

    That distinction is worth remembering: `NULL` is not the same thing as zero or an empty string. A constraint is a rule applied to data or a table—for example, requiring a value or requiring an ID to be unique.

  14. Anna

    One especially important constraint is the primary key. It identifies each row uniquely. If a customer table has an ID column as its primary key, each customer has a distinct ID, so we can refer to a particular record without relying on a name that might be repeated.

  15. Simon

    Relational databases become especially useful when information is split across tables. A customer table might hold customer details, while another table holds product or purchase details. Relationships connect the records in those separate tables.

  16. Anna

    A common way to connect them is to put the parent table’s primary key into a related child table. In the child table, that column is called a foreign key. For example, a `customer_id` in a product or purchase table can refer to an ID in the customer table.

  17. Simon

    The foreign-key relationship helps ensure that a child record refers to a customer that actually exists. It also gives us a reliable way to match related rows when we want information from both tables.

  18. Anna

    Two ideas help describe relationships. Ordinality asks whether a relationship is optional or required. In a relationship diagram, zero can indicate that a related record is optional, while a line can indicate that it is required.

  19. Simon

    Cardinality describes how many records can relate across the tables. The common patterns include one-to-one, one-to-many, and many-to-many. For instance, one parent record might be associated with multiple child records in a one-to-many relationship.

  20. Anna

    Now let’s organize some common SQL commands by their purpose. DDL means Data Definition Language: commands that define or change database structures, such as databases and tables.

  21. Simon

    `CREATE` makes a database or table. `ALTER` changes a table’s structure, such as adding or changing a column or constraint. `DROP` removes the table or database itself, while `TRUNCATE` removes the table’s rows but leaves the table structure in place.

  22. Anna

    That’s an important difference: after `DROP`, the table is gone and has to be created again if you need it. After `TRUNCATE`, the table remains available for new rows.

  23. Simon

    DML, or Data Manipulation Language, is for working with the data in tables. Examples include `INSERT` to add rows, `UPDATE` to change values, `DELETE` to remove rows, and `SELECT` to retrieve data.

  24. Anna

    In MySQL, `USE database_name;` selects a database to work in. SQL also has DCL commands: `GRANT` gives users privileges, and `REVOKE` withdraws privileges that were granted.

  25. Simon

    Then there’s TCL, or Transaction Control Language. `COMMIT` saves transaction changes, `ROLLBACK` undoes uncommitted changes, and `SAVEPOINT` marks a point within a transaction that you can roll back to. `SET TRANSACTION` specifies transaction characteristics.

  26. Anna

    A handy way to remember the everyday data tasks is CRUD: Create, Read, Update, and Delete. In SQL, those ideas correspond broadly to adding data, retrieving it, changing it, and removing it.

  27. Simon

    Let’s put some of this into practice with a MySQL-style workflow. First, `SHOW DATABASES;` lists databases. You can create one with `CREATE DATABASE customer_db;`, then select it with `USE customer_db;`.

  28. Anna

    Once inside the database, you can create a customer table with columns such as `id`, `name`, `age`, `income`, `city`, and `product`. You specify a suitable type for each column, such as an integer for age and a character type for a name.

  29. Simon

    The ID is a good candidate for the primary key. In the example, it is an integer, cannot be `NULL`, and can be set to auto-increment so that MySQL supplies a new ID as rows are added.

  30. Anna

    You can create a second table for product-model details, with columns such as its own ID, name, price, year, and `customer_id`. The `customer_id` column can be made a foreign key referencing the customer table’s ID.

  31. Simon

    After creating tables, `SHOW TABLES;` lets you check which tables are in the selected database. In a multi-line MySQL command, the semicolon marks the end of the statement.

  32. Anna

    To add a customer, use `INSERT INTO` with the table name, the columns you’re supplying, and their values. For example, you might insert a name, age, income, city, and product. Text values are written in quotes; numbers are generally entered as numbers.

  33. Simon

    Then retrieve the row with `SELECT * FROM customer;`. The asterisk means all columns. If you only need a few, write their names instead—for example, `SELECT name, age FROM customer;`.

  34. Anna

    To narrow results, add a `WHERE` clause. `SELECT * FROM customer WHERE city = 'Mumbai';` asks for customer rows whose city is Mumbai. The same idea can target customers in a particular city who bought a particular product.

  35. Simon

    You can combine conditions with `AND`. For example, a query can ask for rows where the city matches one value and the product matches another. That is useful when a table has many records and you need only those that meet several requirements.

  36. Anna

    To sort results, use `ORDER BY`, followed by a column. By default, the example uses ascending order; adding `DESC` sorts from higher values to lower ones. So `ORDER BY income DESC` puts the highest incomes first.

  37. Simon

    SQL also offers aggregate functions for summarizing a column. `SUM(income)` totals income values, `AVG(income)` calculates their average, `MAX(income)` returns the largest value, and `MIN(income)` returns the smallest.

  38. Anna

    You can combine an aggregate with `WHERE` to summarize only matching rows—for example, total income for customers who bought a phone. `COUNT` counts rows or non-null values in the selected expression, so the value you count matters if some entries could be `NULL`.

  39. Simon

    If you want each name only once, use `DISTINCT`, as in `SELECT DISTINCT name FROM customer;`. That removes duplicate values from the result, which is helpful when the same person appears in more than one row.

  40. Anna

    For corrections, use `UPDATE` with `SET` to specify the new values, and use `WHERE` to identify the rows to change. For example, you could correct a customer’s city or income only for the row matching a particular name and product.

  41. Simon

    Be careful with that `WHERE` clause. Without one, an update can affect every row in the table. The same caution applies to `DELETE`: include a condition when you mean to remove only particular records.

  42. Anna

    The demo’s foreign-key constraint connects the product-model table’s `customer_id` to the customer table’s ID. If the constraint is set up to cascade on deletion, deleting a referenced customer can also affect related child rows, according to that cascade rule.

  43. Simon

    Finally, a `JOIN` lets a query combine related information from two tables. You might want a customer’s name, age, and income from the customer table, alongside a model’s name and price from the product-model table.

  44. Anna

    The key is to join on the related columns: the customer table’s ID and the product-model table’s `customer_id`. Then select the columns you want from each table. That gives you one result containing information that was stored separately.

  45. Simon

    As you practice, keep the sequence in mind: create or select a database, define tables and their keys, insert records, and then query, filter, sort, summarize, update, or join the data as needed.

  46. Anna

    For further learning, the session points beginners toward SQL tutorials and blogs, longer video lessons, and courses covering areas such as relational databases, data manipulation, and database platforms. Choose a learning format that suits you, and keep practicing queries against sample tables.

  47. Simon

    That’s the foundation: data is stored in databases, tables organize it into rows and columns, keys connect records, and SQL gives us a way to define structures and work with the data.

  48. Anna

    And once those basics feel comfortable, you can build on them by practicing each command and checking the results. Start with simple `SELECT` queries, add conditions and summaries, and then explore relationships and joins.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent