Introduction to Structured Query Language
Understand SQL fundamentals, key DDL/DML commands, and how to build and filter results with SELECT clauses.
Summary
Read Summary
Flashcards
Save Flashcards
Quiz
Take Quiz
Quick Practice
What is the primary purpose of Structured Query Language (SQL)?
1 of 16
Summary
Fundamentals of Structured Query Language
Introduction
Structured Query Language (SQL) is the universal standard language for communicating with relational databases. Whether you're working with MySQL, PostgreSQL, Oracle, SQL Server, or another database management system, SQL provides a consistent way to create, retrieve, modify, and manage data. Rather than writing low-level code to manipulate files directly, SQL allows you to express your data needs in a high-level, English-like syntax that the database engine interprets and executes efficiently.
Understanding Relational Databases
Before learning SQL, you need to understand the foundation it works with: the relational database structure.
A relational database stores data in tables that resemble spreadsheets. Each table is composed of:
Rows: Each row represents a single record. For example, one row might represent a customer, a student, or a product.
Columns: Each column represents an attribute or property of the records. A customer table might have columns for name, address, phone number, and email.
Think of a student table with columns like studentid, firstname, lastname, and major. Each row in this table would be a different student with their corresponding information in each column.
The Declarative Nature of SQL
One of SQL's most important characteristics is that it is declarative. This means you specify what result you want, not how to obtain it. The database engine figures out the most efficient way to retrieve that result. This is different from imperative programming languages where you must explicitly state each step. For example, you write "Give me all students majoring in Computer Science" rather than writing instructions for the database to search through every row one by one.
Data Definition Language (DDL) Commands
Data Definition Language (DDL) commands define and modify the structure of tables and databases themselves—not the data inside them.
CREATE TABLE
The CREATE TABLE command establishes a new table and specifies its columns, including the data type for each column. This command essentially creates the "container" where data will be stored.
ALTER TABLE
The ALTER TABLE command modifies an existing table's structure. You can use it to add new columns, remove existing columns, or change the data type of a column. This is useful when your data requirements change after a table has been created.
DROP TABLE
The DROP TABLE command deletes an entire table from the database. This is a permanent operation that removes both the table structure and all data within it, so it should be used cautiously.
Key Concept: DDL commands work at the structural level. They don't touch the actual data; they define where and how data will be organized.
Data Manipulation Language (DML) Commands
Data Manipulation Language (DML) commands work with the actual data inside tables. These are the commands you use daily to manage information.
INSERT INTO
The INSERT INTO command adds new rows to a table. Each time you insert a new record—a new customer, a new product, or a new transaction—you're using INSERT.
SELECT
The SELECT command retrieves rows from a table that match specified criteria. This is the most frequently used SQL command and is used for data retrieval. SELECT can return specific columns, specific rows, or both, depending on what you need.
UPDATE
The UPDATE command modifies existing rows by changing the values in specified columns. For example, if a customer changes their address, you would use UPDATE to change that value in the database.
DELETE FROM
The DELETE FROM command removes rows from a table. Unlike DROP TABLE, which removes the entire table, DELETE removes only specific rows based on criteria you specify.
Key Concept: DML commands work with the actual data stored in tables. They do not change the table structure; they change the information contained within.
Diving into the SELECT Statement
The SELECT statement is the most important and complex command in SQL. It retrieves data from your database and is flexible enough to handle many different retrieval scenarios. Understanding its various components is essential.
Selecting Specific Columns
In a SELECT statement, you specify which columns you want returned. Rather than retrieving all columns, you list only the ones you need. For example:
sql
SELECT firstname, lastname FROM students
This returns only the firstname and lastname columns from the students table. If you want all columns, you can use the asterisk wildcard ().
Filtering with the WHERE Clause
The WHERE clause narrows down your results by filtering rows based on conditions you specify. Only rows that meet your condition are returned.
For example:
sql
SELECT firstname, lastname FROM students WHERE major = 'Computer Science'
This returns only the names of students whose major is Computer Science. The WHERE clause can include comparisons like =, >, <, >=, <=, and logical operators like AND and OR to create more complex conditions.
Sorting with the ORDER BY Clause
The ORDER BY clause sorts your result set in a specified order. By default, it sorts in ascending order (A to Z for text, lowest to highest for numbers).
For example:
sql
SELECT firstname, lastname FROM students ORDER BY lastname
This returns students sorted alphabetically by last name. You can specify DESC after the column name to sort in descending order instead.
Grouping with the GROUP BY Clause
The GROUP BY clause groups rows that share a common attribute. This is essential when you want to perform calculations on groups of data rather than individual rows. For instance, if you wanted to count how many students are in each major, you would use GROUP BY to group students by their major, then count the rows in each group.
Combining Tables with the JOIN Clause
The JOIN clause combines rows from multiple tables based on related columns. In relational databases, data is often spread across several tables to avoid redundancy. JOIN allows you to retrieve related information from different tables in a single query.
For example, if you have a students table and a courses table, you might use JOIN to combine a student's information with the courses they're enrolled in, based on a shared student ID column.
This foundation in SQL fundamentals provides the framework for querying and managing relational databases effectively. As you progress, you'll combine these statements in increasingly sophisticated ways to extract meaningful insights from your data.
Flashcards
What is the primary purpose of Structured Query Language (SQL)?
It is the standard language used to communicate with relational databases.
What does it mean for Structured Query Language to be "declarative"?
The user specifies the desired result, and the database engine determines the most efficient way to obtain it.
In what format do relational databases store data?
Tables that resemble spreadsheets.
What does an individual row represent in a relational database table?
An individual record (e.g., a single customer).
What does an individual column represent in a relational database table?
An attribute of a record (e.g., name, address, or phone number).
What is the primary scope or focus of Data Definition Language (DDL) commands?
Defining the structure of the database rather than the data within it.
Which command is used to define a new table and its specific columns?
CREATE TABLE
Which command is used to entirely delete a table from a database?
DROP TABLE
What is the primary scope of Data Manipulation Language (DML) commands?
Working with the actual data stored in tables rather than table definitions.
Which command is used to add new rows to a table?
INSERT INTO
Which command is used to retrieve rows that meet specific criteria?
SELECT
Which command is used to modify existing rows by updating column values?
UPDATE
Which command is used to remove rows from a table?
DELETE FROM
In a SELECT statement, what is the purpose of the WHERE clause?
To filter rows based on a specific condition.
In a SELECT statement, what is the purpose of the GROUP BY clause?
To group rows sharing a common attribute for aggregate calculations.
In a SELECT statement, what is the purpose of the JOIN clause?
To combine rows from multiple tables based on related columns.
Quiz
Introduction to Structured Query Language Quiz Question 1: In a SELECT statement, how do you specify which columns to retrieve?
- List the desired column names after SELECT (correct)
- Use the WHERE clause to name columns
- Place column names in the ORDER BY clause
- Include column names in the GROUP BY clause
Introduction to Structured Query Language Quiz Question 2: Which SQL statement is used to change existing data in a table?
- UPDATE (correct)
- INSERT INTO
- SELECT
- DROP TABLE
Introduction to Structured Query Language Quiz Question 3: What is the primary effect of the DELETE FROM statement?
- It removes rows from a table. (correct)
- It adds new rows to a table.
- It updates existing rows in a table.
- It creates a new table.
Introduction to Structured Query Language Quiz Question 4: How does a relational database organize its data?
- In tables that resemble spreadsheets (correct)
- In hierarchical trees of records
- As unstructured text files
- In a single flat file with delimiters
Introduction to Structured Query Language Quiz Question 5: What is the effect of the DROP TABLE command?
- It deletes the entire table from the database (correct)
- It removes all rows but keeps the table structure
- It renames the table
- It adds a new column to the table
Introduction to Structured Query Language Quiz Question 6: If you need to change the data type of an existing column in a table, which SQL command should you use?
- ALTER TABLE (correct)
- CREATE TABLE
- INSERT INTO
- SELECT
Introduction to Structured Query Language Quiz Question 7: Retrieving rows that satisfy a specific condition belongs to which category of SQL commands?
- Data Manipulation Language (DML) (correct)
- Data Definition Language (DDL)
- Transaction Control Language (TCL)
- Data Control Language (DCL)
Introduction to Structured Query Language Quiz Question 8: Which of the following is a Data Manipulation Language (DML) command?
- INSERT INTO (correct)
- CREATE TABLE
- ALTER TABLE
- DROP TABLE
Introduction to Structured Query Language Quiz Question 9: Which of the following is NOT a component of a SELECT statement?
- DELETE (correct)
- WHERE
- ORDER BY
- GROUP BY
In a SELECT statement, how do you specify which columns to retrieve?
1 of 9
Key Concepts
Database Fundamentals
Relational database
Structured Query Language (SQL)
Data Definition Language (DDL)
Data Manipulation Language (DML)
SQL Query Components
SELECT statement
WHERE clause
ORDER BY clause
GROUP BY clause
JOIN clause
Definitions
Structured Query Language (SQL)
The standard declarative language used to create, modify, retrieve, and manage data in relational database management systems.
Relational database
A type of database that stores data in structured tables with rows and columns, allowing relationships between tables via keys.
Data Definition Language (DDL)
A subset of SQL commands (e.g., CREATE, ALTER, DROP) that define and modify the schema and structure of database objects.
Data Manipulation Language (DML)
A subset of SQL commands (e.g., INSERT, SELECT, UPDATE, DELETE) that operate on the actual data stored within database tables.
SELECT statement
An SQL query that retrieves data from one or more tables, optionally filtering, sorting, grouping, or joining results.
WHERE clause
An SQL component that filters rows returned by a query based on specified logical conditions.
ORDER BY clause
An SQL component that sorts the result set of a query by one or more columns in ascending or descending order.
GROUP BY clause
An SQL component that groups rows sharing common values to enable aggregate calculations such as COUNT or SUM.
JOIN clause
An SQL operation that combines rows from two or more tables based on related columns, supporting various join types (INNER, LEFT, RIGHT, FULL).