Basic SQL Commands


SQL (Structured Query Language) is a programming language used to communicate with databases. It is a standard language for interacting with relational databases and is essential for anyone working with databases. In this article, we will cover some basic SQL commands that you can use to interact with your database.

Introduction to SQL

SQL is a programming language designed specifically for interacting with databases. It allows you to create, modify, and query databases. SQL is used by a wide range of applications, including web applications, data analysis, and data management systems.

It is a declarative language, which means that you specify what you want to do, and the database system figures out how to do it. For example, if you want to retrieve all the rows in a table, you can use the SELECT statement. If you want to insert a new row into a table, you can use the INSERT statement.

It is a standard language, which means that it is supported by most database systems. While there may be slight variations in the syntax from one database system to another, the basic concepts and commands are the same.

Creating a Database

Before you can start working with a database, you need to create it. To create a database, you can use the CREATE DATABASE statement.

CREATE DATABASE database_name;

This statement creates a new database with the specified name. For example, to create a database named "customer_database", you can use the following command −

CREATE DATABASE customer_database;

Creating a Table

Once you have created a database, you can start creating tables. A table is a collection of related data stored in a structured format. Tables are organized into rows and columns, with each column representing a different piece of data and each row representing a unique record.

To create a table, you can use the CREATE TABLE statement.

CREATE TABLE table_name (
   column1 datatype,
   column2 datatype,
   column3 datatype,
   ...
);

This statement creates a new table with the specified name and column definitions. The column definitions specify the name and data type of each column in the table.

For example, to create a table named "customers" with three columns (id, name, and email), you can use the following command −

CREATE TABLE customers (
   id INTEGER PRIMARY KEY,
   name TEXT,
   email TEXT
);

Inserting Data

Once you have created a table, you can start inserting data into it. To insert a new row into a table, you can use the INSERT INTO statement.

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

This statement inserts a new row into the table with the specified values for each column.

For example, to insert a new customer into the "customers" table, you can use the following command −

INSERT INTO customers (name, email)
VALUES ('John Doe', 'john@example.com');

This will insert a new row into the "customers" table with the name "John Doe" and email "john@example.com"

Retrieving Data

One of the most common tasks when working with a database is retrieving data from it. To retrieve data from a table, you can use the SELECT statement.

SELECT column1, column2, ...
FROM table_name;

This statement retrieves all rows from the specified table and returns the values for the specified columns.

For example, to retrieve all customer names from the "customers" table, you can use the following command −

SELECT name
FROM customers;

This will return a list of all customer names in the "customers" table.

You can also specify a condition to filter the rows that are returned. To do this, you can use the WHERE clause.

SELECT column1, column2, ...
FROM table_name
WHERE condition;

The condition is a boolean expression that specifies which rows to include in the result set.

For example, to retrieve all customers with an email ending in "@example.com", you can use the following command −

SELECT name, email
FROM customers
WHERE email LIKE '%@example.com';

This will return a list of all customer names and emails where the email column ends in "@example.com".

Updating Data

Sometimes you may need to update existing data in a table. To update data in a table, you can use the UPDATE statement.

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

This statement updates the values in the specified columns for rows that match the specified condition.

For example, to update the email of all customers with a name starting with "John", you can use the following command −

UPDATE customers
SET email = 'john@newdomain.com'
WHERE name LIKE 'John%';

This will update the email column to "john@newdomain.com" for all rows where the name column starts with "John".

Deleting Data

In some cases, you may need to delete data from a table. To delete data from a table, you can use the DELETE statement.

DELETE FROM table_name
WHERE condition;

This statement deletes all rows from the table that match the specified condition.

For example, to delete all customers with a name starting with "John", you can use the following command −

DELETE FROM customers
WHERE name LIKE 'John%';

This will delete all rows from the "customers" table where the name column starts with "John".

Conclusion

In this article, we covered some basic SQL commands that you can use to interact with a database. We covered how to create a database and a table, insert and retrieve data, update and delete data, and how to use conditions to filter the data you retrieve.

Updated on: 10-Jan-2023

977 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements