Learn Structured Query Language
The w3resource SQL Tutorial is ideal for SQL beginners, including those without prior experience. Our tutorial offers in-depth guidance, covering everything from basic queries like "SELECT * FROM table_name" to complex operations involving multiple tables. It's a comprehensive resource for both novices and those looking to deepen their SQL knowledge.
From the outset, it's crucial to emphasize that our SQL Tutorial adheres strictly to the ANSI SQL:2003 standard. This standardization is essential because when delving into something as significant as SQL, understanding the version or standard you're working with is paramount.
In creating this SQL tutorial, we've integrated a variety of elements to enhance your learning experience. These elements encompass syntax explanations, query dissections, and visual aids aimed at improving SQL comprehension. Furthermore, we've included 1000+ exercises with solutions and explanations with an online code editor and quizzes. This allows you to conveniently practice SQL concepts and queries right in your web browser for a smooth learning journey.
Introduction
In June 1970, a groundbreaking paper titled "A Relational Model of Data for Large Shared Data Banks" was published by Dr. E. F. Codd in the Association of Computer Machinery (ACM) journal. Dr. Codd's model has since become widely recognized as the definitive foundation for relational database management systems (RDBMS).
Drawing upon Codd's model, the language known as Structured English Query Language (SEQUEL) was crafted by IBM Corporation at its San Jose Research Center. Initially, it was called SEQUEL, but it's important to note that the official pronunciation of this language is "ESS QUE ELL."
In a significant development in 1979, Oracle introduced the first commercially available implementation of SQL. This marked the beginning of SQL's journey to becoming the standard language for RDBMS. Subsequently, numerous other players entered the arena, solidifying SQL's position as the prevailing language for relational database management systems today..
Note: If you are not habituated with database management system your can learn from here.
What is SQL?
SQL, which stands for Structured Query Language, is a standardized computer language defined by the ANSI (American National Standards Institute). This language is specifically designed for accessing and manipulating database systems. SQL serves as the backbone for managing data within relational database management systems (RDBMS), where data is organized in tabular form, with relationships between data elements also represented in tables.
SQL statements play a pivotal role in database interaction, allowing users to retrieve, update, and manipulate data seamlessly. SQL is compatible with a wide array of database management systems, including but not limited to DB2, MySQL, PostgreSQL, Oracle, SQLite, SQL Server, Sybase, MS Access, and many others. While there exist various SQL language versions, adherence to the ANSI standard ensures compatibility across different systems. Key SQL keywords such as SELECT, UPDATE, DELETE, INSERT, and WHERE are supported universally within these systems, facilitating consistent and standardized database operations.
The image below illustrates interaction with an RDBMS using SQL. It shows how this language serves as a bridge between users and the underlying database, enabling efficient data management and retrieval.
History of SQL
Here is the year wise development history :
SQL Standard Revisions
SQL standards have shaped this language's capabilities and versatility. Here's a concise overview of key SQL standard revisions:
Constructs of SQL
Here is list of the key elements of SQL along with a brief description:
Some Key terms of SQL
In SQL, statements are grouped into seven categories which are called classes. See the following table :
Class Example SQL data statements SELECT, INSERT, UPDATE, DELETE SQL connection statements CONNECT, DISCONNECT SQL schema statements ALTER, CREATE, DROP SQL control statements CALL, RETURN SQL diagnostic statements GET DIAGNOSTICS SQL session statements SET CONSTRAINT SQL transaction statements COMMIT, ROLLBACKPL-SQL, TSQL and PL/pgSQL
Database and Table Manipulation
Command Description CREATE DATABASE database_name Create a database DROP DATABASE database_name Delete a database CREATE TABLE "table_name" ("column_1" "column_1_data_type", "column_2" "column_2_data_type", ... ) Create a table in a database. ALTER TABLE table_name ADD column_name column_datatype Add columns in an existing table. ALTER TABLE table_name DDROP column_name column_datatype Delete columns in an existing table. DROP TABLE table_name Delete a table.Data Types:
Data Type Description CHARACTER(n) Character string, fixed length n. CHARACTER VARYING(n) orIndex Manipulation:
Command Description CREATE INDEX index_name ON table_name (column_name_1, column_name_2, ...) Create a simple index. CREATE UNIQUE INDEX index_name ON table_name (column_name_1, column_name_2, ...) Create a unique index. DROP INDEX table_name.index_name Drop a index.SQL Operators:
Operators Description SQL Arithmetic Operator Arithmetic operators are addition(+), subtraction(-), multiplication(*) and division(/). The + and - operators can also be used in date arithmetic. SQL Comparison Operator A comparison (or relational) operator is a mathematical symbol which is used to compare two values. SQL Assignment operator In SQL the assignment operator ( = ) assigns a value to a variable or of a column or field of a table. SQL Bitwise Operator The bitwise operators are & ( Bitwise AND ), | ( Bitwise OR ) and ^ ( Bitwise Exclusive OR or XOR ). The valid datatypes for bitwise operators are BINARY, BIT, INT, SMALLINT, TINYINT, and VARBINARY. SQL Logical Operator The Logical operators are those that are true or false. The logical operators are AND , OR, NOT, IN, BETWEEN, ANY, ALL, SOME, EXISTS, and LIKE. SQL Unary Operator The SQL Unary operators perform such an operation which contain only one expression of any of the datatypes in the numeric datatype category.Insert, Update and Delete:
Command Description INSERT INTO table_name VALUES (value_1, value_2,....)Select:
Command DescriptionSELECT column_name(s) FROM table_name
Select data from a table. SELECT * FROM table_name Select all data from a table. SELECT DISTINCT column_name(s) FROM table_name Select only distinct (different) data from a table. SELECT column_name(s) FROM table_name WHERE column operator value AND column operator value OR column operator value AND (... OR ...) ... Select only certain data from a table. SELECT column_name(s) FROM table_name WHERE column_name IN (value1, value2, ...) The IN operator may be used if you know the exact value you want to return for at least one of the columns. SELECT column_name(s) FROM table_name ORDER BY row_1, row_2 DESC, row_3 ASC, ... Select data from a table with sort the rows. SELECT column_1, ..., SUM(group_column_name) FROM table_name GROUP BY group_column_name The GROUP BY clause is used with the SELECT statement to make a group of rows based on the values of a specific column or expression. The SQL AGGREGATE function can be used to get summary information for every group and these are applied to individual group. SELECT column_name(s) INTO new_table_name FROM source_table_name WHERE query Select data from table(S) and insert it into another table. SELECT column_name(s) IN external_database_name FROM source_table_name WHERE query Select data from table(S) and insert it in another database.Functions:
SQL functions Description Aggregate Function This function can produce a single value for an entire group or table. Some Aggregate functions are -Joins:
Name Description SQL EQUI JOIN The SQL EQUI JOIN is a simple SQL join uses the equal sign(=) as the comparison operator for the condition. It has two types - SQL Outer join and SQL Inner join.Union:
Command Description SQL_Statement_1 UNION SQL_Statement_2 Select all different values from SQL_Statement_1 and SQL_Statement_2 SQL_Statement_1 UNION ALL SQL_Statement_2 Select all values from SQL_Statement_1 and SQL_Statement_2View:
Command Description CREATE VIEW view_name AS SELECT column_name(s) FROM table_name WHERE condition Create a virtual table based on the result-set of a SELECT statement.SQL: Frequently Asked Questions
What is SQL used for?
SQL is used for managing and manipulating data in relational database management systems (RDBMS). It allows users to query, update, and delete data from databases.
What are the main components of an SQL statement?
An SQL statement typically consists of clauses like SELECT, FROM, WHERE, and may also include keywords like JOIN, GROUP BY, and ORDER BY, depending on the specific operation.
What's the difference between SQL and MySQL?
SQL is a language for managing databases, while MySQL is a specific database management system that uses SQL as its query language. MySQL is just one of many RDBMS options available.
What are the different types of SQL statements?
SQL statements can broadly be categorized into Data Manipulation Language (DML) statements (e.g., SELECT, INSERT, UPDATE, DELETE), Data Definition Language (DDL) statements (e.g., CREATE, ALTER, DROP), and Data Control Language (DCL) statements (e.g., GRANT, REVOKE).
What are primary keys and foreign keys in SQL?
A primary key is an unique identifier for a record in a table, ensuring each row has a distinct identity. A foreign key, on the other hand, establishes a link between tables, typically referencing the primary key of another table.
What is a SQL JOIN, and how does it work?
SQL JOIN is used to combine rows from two or more tables based on a related column between them. Common joins include INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN.
What are SQL transactions and why are they significant?
SQL transactions are sequences of one or more SQL statements treated as a single unit of work. They are important for ensuring data consistency and integrity, allowing you to make multiple changes to a database as a single operation.
Check out our 1000+ SQL Exercises with solution and explanation to improve your skills.
Next: SQL Data Types
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4