10 Essential SQL Queries Every Developer Should Know

             1. SELECT:

                 The SELECT statement is used to retrieve data from one or more tables in a database. It is the most basic and fundamental                     SQL query. The SELECT statement can be used to retrieve all columns from a table, or only specific columns using the                               SELECT column1, column2 syntax. The WHERE clause can also be added to the SELECT statement to filter records based on                     a condition.

                 Example:

     SELECT * FROM employees;

                 This query retrieves all columns from the “employees” table.

 

             2. WHERE:

                 The WHERE clause is used to filter records based on a condition. It is used with the SELECT, UPDATE, and DELETE                                       statements. The WHERE clause specifies a condition that must be met for the query to return a row. Multiple conditions                           can be combined using AND and OR operators.

                Example:

     SELECT * FROM employees WHERE department = 'sales';

                This query retrieves all columns from the “employees” table where the department is “sales”.

             3. GROUP BY:

                The GROUP BY clause is used to group records based on one or more columns. It is used with the SELECT statement to                           retrieve aggregate data, such as the sum or average of a column, for each group. The GROUP BY clause should always be                          used with an aggregate function, such as SUM or AVG.

                Example:

     SELECT department, AVG(salary) FROM employees GROUP BY department;

                This query retrieves the average salary for each department in the “employees” table.

             4. JOIN:

                 The JOIN clause is used to combine records from two or more tables based on a common column. There are several types of                    JOINs, including INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN. The JOIN clause specifies the columns from each                    table that are used to match records.

                  Example:

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

                This query retrieves the name of each employee along with the name of their department. The INNER JOIN ensures that only                  employees with a department are included in the result set.

             5. ORDER BY:

                The ORDER BY clause is used to sort records based on one or more columns. It is used with the SELECT statement. The                              ORDER BY clause can be used to sort records in ascending or descending order.

                Example:

     SELECT name, salary FROM employees ORDER BY salary DESC;

               This query retrieves the name and salary of each employee in the “employees” table, sorted by salary in descending order.

            6. INSERT:

               The INSERT statement is used to add new records to a table. It is used with the VALUES keyword to specify the values to be                      inserted into each column.

               Example:

    INSERT INTO employees (name, department, salary) VALUES ('John Smith', 'IT', 50000);

               This query inserts a new record into the “employees” table with the name “John Smith”, department “IT”, and salary “50000”.

            7. UPDATE:

                The UPDATE statement is used to modify existing records in a table. It is used with the SET keyword to specify the new values                  for each column, and the WHERE clause to specify which records to update.

                Example:

     UPDATE employees SET salary = 55000 WHERE name = 'John Smith';

               This query updates the salary for the employee named “John Smith” in the “employees” table to “55000”.

            8. DELETE:

               The DELETE statement is used to remove records from a table. It is used with the WHERE clause to specify which records to                     delete.

               Example:

    DELETE FROM employees WHERE name = 'John Smith';

               This query deletes the record for the employee named “John Smith” from employees table.

            9. DISTINCT:

                The DISTINCT keyword is used to remove duplicates from a result set. This query is used to retrieve only unique values from                     a column or set of columns.

 

            10. LIMIT:

                The LIMIT clause is used to limit the number of records returned by a query. This query is used to retrieve a specified                                 number of records from a table.

Leave a Comment

Your email address will not be published. Required fields are marked *