Online SQL Compiler

main.sql
Zoom Theme Undo Redo
Ln 0,Col 0
Input
Output
Download
One input per line:

Intellipaat’s online SQL compiler is an all-in-one compiler for all your SQL queries. It is a great tool that will help you write and run your SQL queries just by using your browser. You can easily practice SQL, test queries, and even manage databases on this compiler without installing anything.

Features of Intellipaat’s Online SQL Compiler

Intellipaat’s online SQL compiler allows you to write, run, and test your SQL queries without installing any database management software. Here are some key features of this compiler:

  • Multiple database support: You can easily run your SQL queries in your browser using Intelipaat’s online SQL compiler.
  • No installation required: There is no need to install anything because this compiler runs on the browser. You just need an internet connection.
  • Easy to use: Intellipaat’s online SQL compiler gives you a user-friendly interface and features like syntax highlighting which makes it easy to use.
  • Tables and database management: You can easily create, modify, and delete tables using this compiler.
  • Query display: When you run a query on this compiler, it will show it in table and chart format.
  • Save and share: You can easily save and share your SQL file using this compiler.

How Online SQL Compiler Works?

Here are step-by-step instructions on how you can write and run your SQL queries using the online SQL compiler:

  1. Open the compiler: First, open the Intellipaat’s online SQL compiler.
  2. Write SQL query: Start writing your SQL query using commands like SELECT, INSERT, UPDATE, and DELETE in the compiler.
  3. Execute: Click on the ‘Run’ button to execute your SQL query.
  4. Output: After running the query, it will show the output in table format and if there is any error then it will show the error.
  5. Update and rerun: If there is any error then make the changes accordingly and run the query again.
  6. Save and share: When you are satisfied with the output, you can save and share it with other people.

Practice on Our Online SQL Compiler

Intellipaat’s online SQL compiler is a great tool to run and write your SQL queries without any setup or installation. It offers you various features to manage your databases. Now we will discuss what is SQL and its important commands and syntax but first, let’s start by creating a simple SQL query.

Write Your First SQL Query in An Online Compiler

Let’s see step-by-step how you can write and run your SQL query in an online compiler:

Step 1: Open the Intellipaat’s online SQL compiler.

Step 2: Once open, type the following query in the editor.

CREATE TABLE Students (

    ID INT,

    Name VARCHAR(50),

    Age INT

);

INSERT INTO Students (ID, Name, Age) VALUES

(1, 'St1', 20),

(2, 'St2', 22),

(3, 'St3', 21);

SELECT * FROM Students;

Step 3: Click on the ‘Run’ button to execute your query.

Step 4: You will get the following output:

ID Name Age
1St120
2St222
3St321

What is SQL?

SQL stands for Structured Query Language. It is a domain-specific language which is used to manage your databases. It can help you with many tasks like storing, finding, updating, and deleting data very easily. You can work with many databases using SQL like MySQL, PostgreSQL, SQL Server, and Oracle.

SQL Syntax

Syntax plays an important role in SQL. You must know all the important syntaxes and commands to write queries without error. Here are some important syntaxes and commands everybody should know to learn SQL:

  • Commands in SQL
    • DQL Command
    • DDL Command
    • DML Command
  • Indexes in SQL
  • Views in SQL
  • Triggers in SQL
  • Stored Procedures in SQL
  • Joins in SQL

Commands in SQL

Commands are used to perform different actions on the database. They are mainly divided into 3 categories based on their functionalities:

1. DQL command

DQL stands for Data Query Language. This command is used to retrieve data from the database.

  • SELECT: It is a DQL command which is used to fetch data from a table.

Syntax:

SELECT column1, column2, ..

FROM table_name

WHERE condition;

Example: In this example, we want the name of the student whose age is greater than 20, where ‘Students’ is the table and ‘Name’ is the column.

SELECT Name, Age FROM Students WHERE Age > 20;

2. DDL command

DDL stands for Data Definition Language. It is used to structure the database like creating a new table, modifying an existing table, or deleting a table.

  • CREATE: This command is used to create a new table.

Syntax:

CREATE TABLE table_name (

    column1 datatype,

    column2 datatype,

    ...);
  • ALTER: This command is used to modify an existing table. You can add, modify, or delete a column from the table.

Syntax:

ALTER TABLE table_name ADD column_name datatype;
  • DROP: This command is used to delete a table or database.

Syntax:

DROP TABLE table_name;
  • TRUNCATE: This command will delete all the data from the table but will keep the structure of the table.

Syntax:

TRUNCATE table table_name;

Note:

Difference between DELETE and TRUNCATE:

  • DELETE removes the rows one by one and it can have a WHERE condition.
  • TRUNCATE is faster but it can not have a WHERE condition.

3. DML command

DML stands for Data Manipulation Language. It is used to modify the database like adding new data, changing the existing data, or removing the data from a table.

  • INSERT: This command is used to insert data in the table.

Syntax:

INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
  • UPDATE: This command is used to change the existing data in a table.

Syntax:

UPDATE table_name

SET column1 = value1, column2 = value2, ..

WHERE condition;
  • DELETE: This command is used to delete the data of a table

Syntax:

DELETE FROM table_name WHERE condition;

Indexes in SQL

Index is a really important concept in SQL which is used to increase the speed of retrieval of data. Indexes can help you with many things like finding data fast without scanning the whole table, reducing the query time when you are working on large tables, and helping with sorting and filtering the data.

For example, if you want to search for an employee by ID in a 1 million rows table, it can take too much time without an index. Adding an index can greatly increase the performance:

CREATE INDEX idx_employee_id ON Employees(id);

SELECT * FROM Employees WHERE id = 1001;

Syntax:

  • To create an index:
CREATE INDEX index_name ON table_name (column_name);
  • To create a unique index:
CREATE UNIQUE INDEX index_name ON table_name (column_name);
  • To remove an index from a table:
DROP INDEX index_name ON table_name;

Views in SQL

A view in SQL is like a saved query which you can use multiple times whenever you need. It doesn’t actually store data but it will show the data from other tables when you run it. It is like a virtual table. You can use views in many different ways like when you have a complicated query, you can save it as a view and run it later, or if you want to show some specific data, you can hide the data you don’t want to show using views.

Syntax:

  • To create a view:
CREATE VIEW view_name AS

SELECT column1, column2, ...

FROM table_name

WHERE condition;
  • To call a view:
SELECT * FROM View_name;
  • To delete a view:
DROP VIEW view_name;

Triggers in SQL

A trigger is a set of commands that automatically run when you perform an action on the database like inserting, updating, or deleting data. There are 2 commonly used triggers in SQL:

  1. BEFORE trigger: It runs before an action happens.
  2. AFTER trigger: It runs after an action happens.

Syntax:

  • To create a trigger:
CREATE TRIGGER trigger_name trigger_time trigger_event

    ON tbl_name FOR EACH ROW [trigger_order] trigger_body

/* where

trigger_time: { BEFORE | AFTER }

trigger_event: { INSERT | UPDATE | DELETE }

trigger_order: { FOLLOWS | PRECEDES } */
  • To delete a trigger:
DROP TRIGGER trigger_name;

Stored Procedures in SQL

A stored procedure in SQL is a saved SQL script that you can run whenever you need. It is like a function that can store multiple SQL statements which makes it good for automating tasks.

Syntax:

  • To create a stored procedure:
CREATE PROCEDURE procedure_name (parameters)

BEGIN

    /*SQL statements here*/

END;
  • To call a stored procedure:
CALL procedure_name;
  • To delete a stored procedure:
DROP PROCEDURE procedure_name;

Joins in SQL

A JOIN is used to combine two or more tables when there are related columns available in all tables. It helps you retrieve the data easily from multiple tables. There are 4 main joins in SQL:

1. INNER JOIN: It will give you the matching data available in both tables.

Syntax:

SELECT * FROM TABLE1 INNER JOIN TABLE2 where condition;

2. LEFT JOIN: It will give you all the data from the left table and the matching data from the right table.

Syntax:

SELECT * FROM TABLE1 LEFT JOIN TABLE2 ON condition;

3. RIGHT JOIN: It will give you all the data from the right table and the matching data from the left table.

Syntax:

SELECT * FROM TABLE1 RIGHT JOIN TABLE2 ON condition;

4. CROSS JOIN: It combines every row of a table with every row of another table. It is also known as a Cartesian Product join.

Syntax:

SELECT select_list from TABLE1 CROSS JOIN TABLE2;

Some Advanced SQL Features

There are some advanced features of SQL that no one talks about. Let’s see them in detail:

1. EXPLAIN Query Optimization

EXPLAIN is used to check query performance by showing the execution plan of the SQL query. It will give you the details of how the query is executed like which indexes are used, the order to scan tables, and the approx. cost.

Syntax:

EXPLAIN SELECT * FROM table_name WHERE condition;

Example: We have a table named “Employees” and we want to know how the query will perform to find out the employees who are older than 30.

EXPLAIN SELECT * FROM Employees WHERE age > 30;

Expected Output:

idselect_typetabletypepossible_keyskeykey_lenrefrowsExtra
1SIMPLEEmployeesrangeidx_ageidx_age5NULL3Using where

2. Stored Procedures vs. Functions

Stored procedures and functions are both types of database objects that allow you to run and save your SQL code. They both help you organize and reuse the SQL logic but they work differently from each other. Let’s see them both one by one:

1. Stored procedures:

A stored procedure is a set of instructions that you can save and run on the database. It is like a mini program in the database.

Key Characteristics:

  • Doesn’t return a value: It helps you with actions like updating, deleting, or inserting data, but it doesn’t give you a value back.
  • Can change data: It can change or modify the data in the database.
  • Can accept input: You can provide a value to it like a person’s ID to work with that specific data.

Example:

For example, if you want to update the salary of an employee. You can do it by creating a stored procedure and you just need to call it when needed.

CREATE PROCEDURE UpdateSalary(IN emp_id INT, IN new_salary DECIMAL)

BEGIN

    UPDATE Employees SET salary = new_salary WHERE id = emp_id;

END;

You can call it like this:

CALL UpdateSalary(1, 5000);

2. Functions:

A function is a set of instructions that is mostly used to calculate or to return a value based on the input.

Key Characteristics:

  • Must return a value: Every function will give you an output like a number or a text.
  • Does not change data: A function can not change the data in the database. It is just used to calculate and return values.
  • Can be used inside queries: You can use functions directly in your SQL queries to calculate the values.

Example:

For example, if you want to find the salary of an employee, you can create a function for that.

CREATE FUNCTION GetSalary(emp_id INT)

RETURNS DECIMAL

BEGIN

    DECLARE salary DECIMAL;

    SELECT salary INTO salary FROM Employees WHERE id = emp_id;

    RETURN salary;

END;

You can then use it in a query like this:

SELECT name, GetSalary(id) FROM Employees WHERE id = 1;

When to use?

  • Stored procedure: You can use this when you want to update or delete data from the database.
  • Function: You can use functions when you want to calculate something and get values based on the input.

So far we have learned what an online SQL compiler is and what features Intellipaat’s online SQL compiler provides. We also discussed what is SQL and all the important commands and syntaxes including indexes, views, triggers, and joins. If you are interested in learning more about SQL, you can check out our amazing SQL training program.

FAQs – Online SQL Compiler

1. What is an online SQL compiler?

An online SQL compiler is a tool that will help you write and run your SQL queries just by using your browser. You can easily practice SQL, test queries, and even manage databases on this compiler.

2. Is Intellipaat’s online SQL compiler easy to use?

Yes, Intellipaat’s online SQL compiler is easy to use because it gives you a user-friendly interface with features like syntax highlighting.

3. What are the benefits of using an online SQL compiler?

The key benefits of using an online SQL compiler are:

  • Simple and easy to use: The online compiler gives you a user-friendly interface to write and run your SQL queries. It’s great for learning and practicing SQL.
  • No installation required: The online compiler runs on the web browser so you don’t need any setup to work on it.
  • Multiple database support: The online compiler supports multiple databases so you can easily work with MySQL, PostgreSQL, SQL Server, etc.
  • Instant query execution: You can quickly execute and test your SQL queries in real time.

4. What is the difference between DELETE and TRUNCATE?

DELETE removes the rows one by one and it can have a WHERE clause whereas TRUNCATE removes all rows instantly and it is faster but it can not have a WHERE clause.

5. What are the limitations of using an online SQL compiler?

There are some limitations in an online SQL compiler which are:

  • You will need an internet connection to run queries on an online compiler.
  • The online compiler can be slower than the local databases especially when you are working with larger sizes of data.
  • Due to the limited storage space, many online compilers don’t execute large databases.
  • It can not connect to local servers.