WikiGalaxy

Personalize

SQL SELECT Statement

Purpose:

The SELECT statement is used to select data from a database. The data returned is stored in a result table, sometimes called the result set.

Basic Syntax:

The basic syntax of the SELECT statement is: SELECT column1, column2, ... FROM table_name;


SELECT first_name, last_name FROM employees;
    

Example Explanation:

This example selects the first and last names of all employees from the "employees" table.

Console Output:

John Doe

Jane Smith

SQL WHERE Clause

Purpose:

The WHERE clause is used to filter records. It is used to extract only those records that fulfill a specified condition.

Basic Syntax:

The basic syntax of the WHERE clause is: SELECT column1, column2, ... FROM table_name WHERE condition;


SELECT * FROM employees WHERE department = 'Sales';
    

Example Explanation:

This example selects all columns from the "employees" table where the department is "Sales".

Console Output:

ID: 1, Name: John Doe, Department: Sales

SQL INSERT INTO Statement

Purpose:

The INSERT INTO statement is used to insert new records in a table.

Basic Syntax:

The basic syntax of the INSERT INTO statement is: INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);


INSERT INTO employees (first_name, last_name, department) VALUES ('Alice', 'Wonderland', 'HR');
    

Example Explanation:

This example inserts a new record into the "employees" table with the first name "Alice", last name "Wonderland", and department "HR".

Console Output:

Record inserted successfully.

SQL UPDATE Statement

Purpose:

The UPDATE statement is used to modify the existing records in a table.

Basic Syntax:

The basic syntax of the UPDATE statement is: UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;


UPDATE employees SET department = 'Marketing' WHERE first_name = 'Alice';
    

Example Explanation:

This example updates the department of the employee with the first name "Alice" to "Marketing".

Console Output:

Record updated successfully.

SQL DELETE Statement

Purpose:

The DELETE statement is used to delete existing records in a table.

Basic Syntax:

The basic syntax of the DELETE statement is: DELETE FROM table_name WHERE condition;


DELETE FROM employees WHERE first_name = 'Alice';
    

Example Explanation:

This example deletes the record of the employee whose first name is "Alice".

Console Output:

Record deleted successfully.

SQL ORDER BY Clause

Purpose:

The ORDER BY keyword is used to sort the result set in ascending or descending order.

Basic Syntax:

The basic syntax of the ORDER BY clause is: SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ... ASC|DESC;


SELECT first_name, last_name FROM employees ORDER BY last_name DESC;
    

Example Explanation:

This example selects the first and last names of employees and sorts them by last name in descending order.

Console Output:

Smith, Jane

Doe, John

SQL GROUP BY Clause

Purpose:

The GROUP BY statement is used in conjunction with aggregate functions to group the result set by one or more columns.

Basic Syntax:

The basic syntax of the GROUP BY clause is: SELECT column1, aggregate_function(column2) FROM table_name GROUP BY column1;


SELECT department, COUNT(*) FROM employees GROUP BY department;
    

Example Explanation:

This example counts the number of employees in each department.

Console Output:

Sales: 5

HR: 3

SQL JOIN Clause

Purpose:

The JOIN clause is used to combine rows from two or more tables, based on a related column between them.

Basic Syntax:

The basic syntax of the JOIN clause is: SELECT columns FROM table1 INNER JOIN table2 ON table1.column = table2.column;


SELECT employees.first_name, departments.department_name FROM employees INNER JOIN departments ON employees.department_id = departments.id;
    

Example Explanation:

This example selects the first names of employees along with their department names by joining the "employees" and "departments" tables.

Console Output:

John, Sales

Jane, HR

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025