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

Power BI tenant settings and admin portal

As of my last update, Power BI offers a dedicated admin portal for managing settings and configurations at the tenant level. Here's an overview of Power BI tenant settings and the admin portal: 1. Power BI Admin Portal: Access : The Power BI admin portal is accessible to users with admin privileges in the Power BI service. URL : You can access the admin portal at https://app.powerbi.com/admin-portal . 2. Tenant Settings: General Settings : Configure general settings such as tenant name, regional settings, and language settings. Tenant Administration : Manage user licenses, permissions, and access rights for Power BI within the organization. Usage Metrics : View usage metrics and reports to understand how Power BI is being used across the organization. Service Health : Monitor the health status of the Power BI service and receive notifications about service incidents and outages. Audit Logs : Access audit logs to track user activities, access requests, and administrative actions wit...

Using bookmarks and buttons for navigation

Using bookmarks and buttons for navigation in Power BI allows you to create interactive experiences within your reports, guiding users through different views and sections. Let's walk through how to use bookmarks and buttons for navigation: Step 1: Create Bookmarks Navigate to the "View" tab : Open your report in Power BI Desktop and navigate to the "View" tab. Create Bookmarks : Select the elements (visuals, slicers, shapes, etc.) that you want to bookmark. Click on the "Bookmark" button in the "View" tab or right-click and select "Add bookmark". Name your bookmark and ensure the "Data" and "Display" options are selected if you want to capture filter states and visual display states. Repeat for Additional Views : Create bookmarks for each view or section of your report that you want to navigate to. Step 2: Create Buttons Insert Buttons : Go to the "Home" tab and click on the "Buttons" dropdow...

Understanding the Power BI ecosystem and workflow

Understanding the Power BI ecosystem and workflow involves getting familiar with the various components of Power BI and how they interact to provide a comprehensive data analysis and visualization solution. Here's a detailed explanation: Power BI Ecosystem The Power BI ecosystem consists of several interconnected components that work together to enable users to connect to data sources, transform and model data, create visualizations, and share insights. The main components are: Power BI Desktop Power BI Service Power BI Mobile Power BI Gateway Power BI Report Server Power BI Embedded PowerBI Workflow Here’s a typical workflow in the Power BI ecosystem: Step 1: Connect to Data Sources Power BI Desktop:  Connect to various data sources like Excel, SQL databases, cloud services, and more. Power BI Gateway:  If using on-premises data sources, install and configure the gateway for secure data transfer. Step 2: Data Transformation and Modeling Power BI Desktop:  Use Power Query...