Skip to main content

Introduction to SQL

SQL (Structured Query Language) is a standardized programming language designed for managing and manipulating relational databases. It allows users to perform various operations such as querying data, updating records, and managing database structures. Here’s a detailed introduction to SQL:


What is SQL?

SQL is used for:

  • Defining database schemas (DDL).
  • Manipulating data within those schemas (DML).
  • Controlling access to the data (DCL).
  • Ensuring data integrity and managing transactions (TCL).

 

History of SQL

  • 1970: Dr. Edgar F. Codd introduced the relational model for databases.
  • 1974: SQL was developed by IBM researchers Raymond Boyce and Donald Chamberlin.
  • 1979: Oracle released the first commercial SQL-based RDBMS.
  • 1986: SQL was standardized by ANSI and ISO.

 

SQL Syntax

SQL syntax is relatively straightforward. Here’s an example of the basic SQL structure:

SELECT column1, column2

FROM table_name

WHERE condition

ORDER BY column1, column2;

 

 

Key SQL Commands

Data Definition Language (DDL)

CREATE: Used to create databases and tables.

CREATE TABLE employees (

    id INT PRIMARY KEY,

    name VARCHAR(100),

    department VARCHAR(100),

    salary DECIMAL(10, 2)

);

 

ALTER: Used to modify an existing database object.

ALTER TABLE employees

ADD COLUMN hire_date DATE;

 

DROP: Used to delete a table or database.

DROP TABLE employee;

 

 

Data Manipulation Language (DML)

SELECT: Used to retrieve data from a database.

SELECT name, department FROM employees WHERE salary > 50000;

 

INSERT: Used to add new records to a table.

INSERT INTO employees (id, name, department, salary)

VALUES (1, 'John Doe', 'Engineering', 75000);

 

UPDATE: Used to modify existing records.

UPDATE employees SET salary = 80000 WHERE id = 1;

 

DELETE: Used to remove records from a table. 

DELETE FROM employees WHERE id = 1;

 

Data Control Language (DCL)

GRANT: Used to provide user access privileges.

GRANT SELECT, INSERT ON employees TO user1;


REVOKE: Used to remove user access privileges.

REVOKE SELECT, INSERT ON employees FROM user1;

 

Transaction Control Language (TCL)

COMMIT: Used to save all transactions to the database.

COMMIT;


ROLLBACK: Used to undo transactions that have not yet been saved.

ROLLBACK;


SAVEPOINT: Sets a savepoint within a transaction.

SAVEPOINT savepoint1;

 

Basic SQL Concepts


Data Types

Different databases support different data types. Common SQL data types include:

INTEGER: Whole numbers.

VARCHAR(size): Variable length string.

CHAR(size): Fixed length string.

DATE: Date values.

DECIMAL(p, s): Exact numeric values.

 


Primary Key

A primary key uniquely identifies each record in a table. It must contain unique values and cannot contain NULL values.

CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(100) );

 

Foreign Key

A foreign key is a field (or collection of fields) in one table that uniquely identifies a row in another table.

CREATE TABLE orders ( order_id INT PRIMARY KEY, employee_id INT, FOREIGN KEY (employee_id) REFERENCES employees(id) );

 

Indexes

Indexes are used to retrieve data from the database more quickly.

CREATE INDEX idx_name ON employees(name);

 

Conclusion

SQL is a powerful tool for managing and manipulating relational databases. Its standardized syntax and broad support across various database systems make it an essential skill for database administrators, developers, and data analysts. This introduction provides a foundation for further exploration and mastery of SQL.

 

 

 

 


Comments

Popular posts from this blog

TechUplift: Elevating Your Expertise in Every Click

  Unlock the potential of data with SQL Fundamental: Master querying, managing, and manipulating databases effortlessly. Empower your database mastery with PL/SQL: Unleash the full potential of Oracle databases through advanced programming and optimization. Unlock the Potential of Programming for Innovation and Efficiency.  Transform raw data into actionable insights effortlessly. Empower Your Data Strategy with Power Dataware: Unleash the Potential of Data for Strategic Insights and Decision Making.

SQL Fundamentals

SQL, or Structured Query Language, is the go-to language for managing relational databases. It allows users to interact with databases to retrieve, manipulate, and control data efficiently. SQL provides a standardized way to define database structures, perform data operations, and ensure data integrity. From querying data to managing access and transactions, SQL is a fundamental tool for anyone working with databases. 1. Basics of SQL Introduction : SQL (Structured Query Language) is used for managing and manipulating relational databases. SQL Syntax : Basic structure of SQL statements (e.g., SELECT, INSERT, UPDATE, DELETE). Data Types : Different types of data that can be stored (e.g., INTEGER, VARCHAR, DATE). 2. SQL Commands DDL (Data Definition Language) : CREATE TABLE : Define new tables. ALTER TABLE : Modify existing tables. DROP TABLE : Delete tables. DML (Data Manipulation Language) : INSERT : Add new records. UPDATE : Modify existing records. DELETE : Remove records. DQL (Da...

DAX Functions

These are just some of the many DAX functions available in Power BI. Each  function serves a specific purpose and can be used to perform a wide range of calculations and transformations on your data. Aggregation Functions: SUM : Calculates the sum of values. AVERAGE : Calculates the arithmetic mean of values. MIN : Returns the smallest value in a column. MAX : Returns the largest value in a column. COUNT : Counts the number of rows in a table or column. COUNTA : Counts the number of non-blank values in a column. DISTINCTCOUNT : Counts the number of unique values in a column. Logical Functions: IF : Returns one value if a condition is true and another value if it's false. AND : Returns TRUE if all the conditions are true, otherwise FALSE. OR : Returns TRUE if any of the conditions are true, otherwise FALSE. NOT : Returns the opposite of a logical value. Text Functions: CONCATENATE : Concatenates strings together. LEFT : Returns the leftmost characters from a text string. RIGHT : Ret...