Skip to main content

SQL Syntax

SQL (Structured Query Language) is a programming language used for managing and manipulating relational databases. Here's a basic overview of its syntax:


  1. SELECT: Used to retrieve data from a database.

SELECT column1, column2 FROM table_name WHERE condition;


Example


SELECT first_name, last_name FROM employees WHERE department = 'HR';




  1. INSERT INTO: Used to insert new records into a table.

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


Example


INSERT INTO employees (first_name, last_name) VALUES ('John', 'Doe');



  1. UPDATE: Used to modify existing records in a table.

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


Example


UPDATE employees SET department = 'Finance' WHERE employee_id = 101;


  1. DELETE: Used to delete records from a table.

DELETE FROM table_name WHERE condition;


Example


DELETE FROM employee WHERE employee_id=102;


  1. CREATE TABLE: Used to create a new table in the database.

CREATE TABLE table_name (

    column1 datatype,

    column2 datatype,

    ...

);


Example


CREATE TABLE employees ( employee_id INT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), department VARCHAR(50) );



  1. ALTER TABLE: Used to modify an existing table.

ALTER TABLE table_name ADD column_name datatype;


Example


ALTER TABLE employees ADD email VARCHAR(100);



  1. DROP TABLE: Used to delete a table and all its data from the database.

DROP TABLE table_name;


Example


DROP TABLE table_name;



  1. JOIN: Used to combine rows from two or more tables based on a related column.

SELECT * FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;



Example


SELECT orders.order_id, customers.customer_name FROM orders INNER JOIN customers ON orders.customer_id = customers.customer_id;




  1. GROUP BY: Used to group rows that have the same values into summary rows.

SELECT column1, COUNT(column2) FROM table_name GROUP BY column1;


Example


SELECT product_id, SUM(quantity) AS total_quantity FROM orders GROUP BY product_id;



  1. ORDER BY: Used to sort the result set in ascending or descending order.

SELECT * FROM table_name ORDER BY column_name ASC|DESC;


Example


SELECT * FROM customers ORDER BY last_name ASC;



These are some of the fundamental SQL commands and their syntax. The exact syntax may vary slightly depending on the database management system you are using (e.g., MySQL, PostgreSQL, SQLite, etc.), so it's always a good idea to consult the documentation specific to your database.

 

Comments

Popular posts from this blog

Performance Optimization

Performance optimization in SQL is crucial for ensuring that your database queries run efficiently, especially as the size and complexity of your data grow. Here are several strategies and techniques to optimize SQL performance: Indexing Create Indexes : Primary Key and Unique Indexes : These are automatically indexed. Ensure that your tables have primary keys and unique constraints where applicable. Foreign Keys : Index foreign key columns to speed up join operations. Composite Indexes : Use these when queries filter on multiple columns. The order of columns in the index should match the order in the query conditions. Avoid Over-Indexing:  Too many indexes can slow down write operations (INSERT, UPDATE, DELETE). Only index columns that are frequently used in WHERE clauses, JOIN conditions, and as sorting keys. Query Optimization Use SELECT Statements Efficiently : SELECT Only Necessary Columns : Avoid using SELECT * ; specify only ...

DAX UPPER Function

The DAX UPPER function in Power BI is used to convert all characters in a text string to uppercase. This function is useful for standardizing text data, ensuring consistency in text values, and performing case-insensitive comparisons. Syntax: UPPER(<text>) <text>: The text string that you want to convert to uppercase. Purpose: The UPPER function helps ensure that text data is consistently formatted in uppercase. This can be essential for tasks like data cleaning, preparing text for comparisons, and ensuring uniformity in text-based fields. E xample: Suppose you have a table named "Customers" with a column "Name" that contains names in mixed case. You want to create a new column that shows all names in uppercase. UppercaseName = UPPER(Customers[Name]) Example Scenario: Assume you have the following "Customers" table: You can use the UPPER function as follows: Using the UPPER function, you can convert all names to uppercase: UppercaseName = ...

TechUplift: Elevating Your Expertise in Every Click

  Unlock the potential of data with SQL Fundamental: Master querying, managing, and manipulating databases effortlessly. Empower your database mastery with PL/SQL: Unleash the full potential of Oracle databases through advanced programming and optimization. Unlock the Potential of Programming for Innovation and Efficiency.  Transform raw data into actionable insights effortlessly. Empower Your Data Strategy with Power Dataware: Unleash the Potential of Data for Strategic Insights and Decision Making.