SQL Keywords Reference

beginner
20 min

SQL Keywords Reference

Welcome to our deep dive into SQL (Structured Query Language)! This tutorial is designed to guide you through the most essential SQL keywords. Let's embark on this exciting journey together, learning SQL with practical examples and real-world applications.

Getting Started 🎯

Before we delve into the keywords, let's first understand what SQL is and why it's important.

SQL is a standard language used to communicate with and manipulate databases. It allows us to create, modify, and retrieve data stored in a database, making it an indispensable tool for developers and data analysts.

Now, let's familiarize ourselves with some fundamental SQL concepts.

Basic Keywords 📝

  1. SELECT - Retrieves data from a database

    sql
    SELECT column1, column2 FROM table_name;

    This SQL statement retrieves data from the specified table (table_name) for the specified columns (column1, column2).

    Example: Retrieve all records from the customers table with the name and email columns.

    sql
    SELECT name, email FROM customers;
  2. FROM - Specifies the table to query

    As shown in the example above, the FROM keyword is used to specify the table from which data will be retrieved.

  3. WHERE - Filters the data based on conditions

    sql
    SELECT column1, column2 FROM table_name WHERE condition;

    This SQL statement retrieves data from the specified table (table_name) for the specified columns (column1, column2) where the condition is met.

    Example: Retrieve all records from the orders table where the order total exceeds 100.

    sql
    SELECT order_id, total FROM orders WHERE total > 100;

Intermediate Keywords 💡

Now that we've covered the basics, let's move on to some intermediate SQL keywords.

  1. AND, OR, NOT - Combines conditions for more precise filtering

    Use these keywords to combine conditions and create more complex filtering rules.

    Example: Retrieve all records from the customers table where the customer is from 'USA' or 'Canada' and has an email address ending with '.com'.

    sql
    SELECT name, email FROM customers WHERE country IN ('USA', 'Canada') AND email LIKE '%.com';
  2. LIKE - Searches for specific patterns in data

    Use the LIKE keyword to search for specific patterns within the data.

    Example: Retrieve all records from the orders table where the order number contains the digits 123.

    sql
    SELECT order_id, total FROM orders WHERE order_id LIKE '%123%';

Advanced Keywords 🎯

As you progress, you'll encounter advanced SQL keywords that allow you to manipulate data in more sophisticated ways.

  1. JOIN - Combines rows from two or more tables

    Use the JOIN keyword to combine rows from two or more tables based on a related column.

    Example: Retrieve all orders and the corresponding customer details.

    sql
    SELECT customers.name, orders.order_id, orders.total FROM customers JOIN orders ON customers.customer_id = orders.customer_id;
  2. GROUP BY - Groups rows based on a specified column

    Use the GROUP BY keyword to group rows based on a specified column and perform aggregate functions on the groups.

    Example: Retrieve the total sales for each customer.

    sql
    SELECT customers.name, SUM(orders.total) AS total_sales FROM customers JOIN orders ON customers.customer_id = orders.customer_id GROUP BY customers.name;
Quick Quiz
Question 1 of 1

What does the `SELECT` keyword do in SQL?

Quick Quiz
Question 1 of 1

What does the `FROM` keyword do in SQL?

Happy learning! As you continue exploring SQL, remember to practice regularly and experiment with different queries. This will help you gain a deeper understanding of SQL and make you more proficient in handling databases. 🎯💡📝✅