avatarSQL Fundamentals

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3383

Abstract

/span> price <span class="hljs-operator">=</span> <span class="hljs-number">25.99</span> <span class="hljs-keyword">WHERE</span> product_id <span class="hljs-operator">=</span> <span class="hljs-number">101</span>;

<span class="hljs-comment">-- Update multiple rows</span> <span class="hljs-keyword">UPDATE</span> employees <span class="hljs-keyword">SET</span> manager_id <span class="hljs-operator">=</span> <span class="hljs-number">105</span> <span class="hljs-keyword">WHERE</span> department <span class="hljs-operator">=</span> <span class="hljs-string">'Sales'</span>;</pre></div><h1 id="9282">5. DELETE Statement</h1><h1 id="e0de">Removing Data</h1><p id="612f">The <code>DELETE</code> statement is used to remove rows from a table.</p><div id="718a"><pre><span class="hljs-comment">-- Delete a single row</span> <span class="hljs-keyword">DELETE</span> <span class="hljs-keyword">FROM</span> customers <span class="hljs-keyword">WHERE</span> customer_id <span class="hljs-operator">=</span> <span class="hljs-number">201</span>;

<span class="hljs-comment">-- Delete all rows that meet a condition</span> <span class="hljs-keyword">DELETE</span> <span class="hljs-keyword">FROM</span> orders <span class="hljs-keyword">WHERE</span> order_date <span class="hljs-operator"><</span> <span class="hljs-string">'2023-01-15'</span>;</pre></div><h1 id="0a71">6. CREATE TABLE Statement</h1><h1 id="6ccf">Creating New Tables</h1><p id="1b48">The <code>CREATE TABLE</code> statement is used to create a new table with specified columns and data types.</p><div id="92d4"><pre><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> products ( product_id <span class="hljs-type">INT</span> <span class="hljs-keyword">PRIMARY</span> KEY, product_name <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">255</span>), price <span class="hljs-type">DECIMAL</span>(<span class="hljs-number">10</span>, <span class="hljs-number">2</span>) );</pre></div><h1 id="5500">7. ALTER TABLE Statement</h1><h1 id="b09f">Modifying Tables</h1><p id="fd23">The <code>ALTER TABLE</code> statement allows you to modify an existing table by adding, modifying, or deleting columns.</p><div id="8d31"><pre><span class="hljs-comment">-- Add a new column</span> <span class="hljs-keyword">ALTER</span> <span class="hljs-keyword">TABLE</span> employees <span class="hljs-keyword">ADD</span> <span class="hljs-keyword">COLUMN</span> hire_date <span class="hljs-type">DATE</span>;

<span class="hljs-comment">-- Modify column data type</span> <span class="hljs-keyword">ALTER</span> <span class="hljs-keyword">TABLE</span> customers <span class="hljs-keyword">ALTER</span> <span class="hljs-keyword">COLUMN</span> phone_number <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">15</span>);</pre></div><h1 id="956a">8. DROP TABLE Statement</h1><h1 id="c04f">Deleting Tables</h1><p id="ce2f">The <code>DROP TABLE</code> statement is used to delete an existing table and all its data.</p><div id="2bea"><pre><span class="hljs-comment">-- Delete a table</span> <span class="hljs-keyword">DROP</span> <span class="hljs-keyword">TABLE</span> products;</pre></div><h1 id="4ef7">9. WHERE Clause</h1><h1 id="e050">Filtering Data</h1><p id="9ce5">The <code>WHERE</code> clause is used to filter rows based on specified conditions.</p><div id="5514"><pre><s

Options

pan class="hljs-comment">-- Retrieve products with a price greater than 50</span> <span class="hljs-keyword">SELECT</span> product_name, price <span class="hljs-keyword">FROM</span> products <span class="hljs-keyword">WHERE</span> price <span class="hljs-operator">></span> <span class="hljs-number">50</span>;

<span class="hljs-comment">-- Retrieve employees in the Sales department</span> <span class="hljs-keyword">SELECT</span> first_name, last_name <span class="hljs-keyword">FROM</span> employees <span class="hljs-keyword">WHERE</span> department <span class="hljs-operator">=</span> <span class="hljs-string">'Sales'</span>;</pre></div><h1 id="a9f1">10. JOIN Clause</h1><h1 id="ff1a">Combining Data from Multiple Tables</h1><p id="743f">The <code>JOIN</code> clause is used to combine rows from two or more tables based on a related column between them.</p><div id="b52d"><pre><span class="hljs-comment">-- Retrieve customer names and their orders</span> <span class="hljs-keyword">SELECT</span> c.first_name, c.last_name, o.order_date <span class="hljs-keyword">FROM</span> customers <span class="hljs-keyword">AS</span> c <span class="hljs-keyword">JOIN</span> orders <span class="hljs-keyword">AS</span> o <span class="hljs-keyword">ON</span> c.customer_id <span class="hljs-operator">=</span> o.customer_id;</pre></div><h1 id="fa7b">11. GROUP BY Clause</h1><h1 id="72f5">Aggregating Data</h1><p id="0de8">The <code>GROUP BY</code> clause is used to group rows that have the same values in specified columns, often used with aggregate functions like <code>SUM</code> and <code>COUNT</code>.</p><div id="d7d1"><pre><span class="hljs-comment">-- Calculate total sales per product</span> <span class="hljs-keyword">SELECT</span> product_id, <span class="hljs-built_in">SUM</span>(quantity <span class="hljs-operator">*</span> price) <span class="hljs-keyword">AS</span> total_sales <span class="hljs-keyword">FROM</span> order_details <span class="hljs-keyword">GROUP</span> <span class="hljs-keyword">BY</span> product_id;</pre></div><h1 id="4194">12. Conclusion</h1><h1 id="8dd3">Mastering the Basics</h1><p id="bced">These 10 SQL statements cover the majority of tasks you’ll encounter when working with relational databases. By understanding and becoming proficient with these statements, you’ll have a solid foundation for database management and data manipulation. SQL is a versatile language, and as you continue to explore its capabilities, you’ll unlock even more powerful ways to work with data.</p><h1 id="078f">SQL Fundamentals</h1><p id="1eb0"><i>Thank you for your time and interest! <b>🚀 </b>You can find even more content at <a href="https://medium.com/@sqlfundamentals"><b>SQL Fundamentals </b></a><b>💫</b></i></p><h1 id="2ddf">Stackademic</h1><p id="75a8"><i>Thank you for reading until the end. Before you go:</i></p><ul><li><i>Please consider <b>clapping</b> and <b>following</b> the writer! 👏</i></li><li><i>Follow us on <a href="https://twitter.com/stackademichq"><b>Twitter(X)</b></a>, <a href="https://www.linkedin.com/company/stackademic"><b>LinkedIn</b></a>, and <a href="https://www.youtube.com/c/stackademic"><b>YouTube</b></a><b>.</b></i></li><li><i>Visit <a href="http://stackademic.com/"><b>Stackademic.com</b></a> to find out more about how we are democratizing free programming education around the world.</i></li></ul></article></body>

10 SQL Statements That Can Handle 90% of Tasks

Structured Query Language (SQL) is a powerful tool for managing and querying relational databases. Whether you’re a beginner or an experienced data professional, there are certain SQL statements that you’ll find yourself using over and over again. In this article, we’ll cover 10 essential SQL statements that can handle 90% of your database tasks, along with code examples.

1. Introduction

Why SQL is Important

SQL is the standard language for interacting with relational databases. It allows you to perform various tasks, from retrieving data to modifying database structures. Understanding SQL is essential for anyone working with data, as it provides a foundation for data analysis, reporting, and application development.

2. SELECT Statement

Retrieving Data

The SELECT statement is used to retrieve data from one or more tables. You can specify which columns to retrieve and add conditions to filter the results.

-- Retrieve all columns from a table
SELECT * FROM employees;

-- Retrieve specific columns
SELECT first_name, last_name FROM employees;

-- Add conditions to filter the results
SELECT product_name, price FROM products WHERE price > 50;

3. INSERT INTO Statement

Adding New Data

The INSERT INTO statement allows you to add new rows of data to a table.

-- Insert a single row
INSERT INTO customers (first_name, last_name, email) VALUES ('John', 'Doe', '[email protected]');

-- Insert multiple rows
INSERT INTO orders (order_date, total_amount) VALUES
    ('2023-01-15', 150.00),
    ('2023-01-16', 220.50),
    ('2023-01-17', 75.25);

4. UPDATE Statement

Modifying Existing Data

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

-- Update a single row
UPDATE products SET price = 25.99 WHERE product_id = 101;

-- Update multiple rows
UPDATE employees SET manager_id = 105 WHERE department = 'Sales';

5. DELETE Statement

Removing Data

The DELETE statement is used to remove rows from a table.

-- Delete a single row
DELETE FROM customers WHERE customer_id = 201;

-- Delete all rows that meet a condition
DELETE FROM orders WHERE order_date < '2023-01-15';

6. CREATE TABLE Statement

Creating New Tables

The CREATE TABLE statement is used to create a new table with specified columns and data types.

CREATE TABLE products (
    product_id INT PRIMARY KEY,
    product_name VARCHAR(255),
    price DECIMAL(10, 2)
);

7. ALTER TABLE Statement

Modifying Tables

The ALTER TABLE statement allows you to modify an existing table by adding, modifying, or deleting columns.

-- Add a new column
ALTER TABLE employees ADD COLUMN hire_date DATE;

-- Modify column data type
ALTER TABLE customers ALTER COLUMN phone_number VARCHAR(15);

8. DROP TABLE Statement

Deleting Tables

The DROP TABLE statement is used to delete an existing table and all its data.

-- Delete a table
DROP TABLE products;

9. WHERE Clause

Filtering Data

The WHERE clause is used to filter rows based on specified conditions.

-- Retrieve products with a price greater than 50
SELECT product_name, price FROM products WHERE price > 50;

-- Retrieve employees in the Sales department
SELECT first_name, last_name FROM employees WHERE department = 'Sales';

10. JOIN Clause

Combining Data from Multiple Tables

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

-- Retrieve customer names and their orders
SELECT c.first_name, c.last_name, o.order_date
FROM customers AS c
JOIN orders AS o ON c.customer_id = o.customer_id;

11. GROUP BY Clause

Aggregating Data

The GROUP BY clause is used to group rows that have the same values in specified columns, often used with aggregate functions like SUM and COUNT.

-- Calculate total sales per product
SELECT product_id, SUM(quantity * price) AS total_sales
FROM order_details
GROUP BY product_id;

12. Conclusion

Mastering the Basics

These 10 SQL statements cover the majority of tasks you’ll encounter when working with relational databases. By understanding and becoming proficient with these statements, you’ll have a solid foundation for database management and data manipulation. SQL is a versatile language, and as you continue to explore its capabilities, you’ll unlock even more powerful ways to work with data.

SQL Fundamentals

Thank you for your time and interest! 🚀 You can find even more content at SQL Fundamentals 💫

Stackademic

Thank you for reading until the end. Before you go:

  • Please consider clapping and following the writer! 👏
  • Follow us on Twitter(X), LinkedIn, and YouTube.
  • Visit Stackademic.com to find out more about how we are democratizing free programming education around the world.
Sql
MySQL
Data Science
Data Analysis
Data Analytics
Recommended from ReadMedium