Sunday, July 30, 2023

MySQL INSERT INTO SELECT Statement

MySQL INSERT INTO SELECT Statement

The INSERT INTO SELECT statement copies data from one table and inserts it into another table.

The INSERT INTO SELECT statement requires that the data types in source and target tables matches.

Note: The existing records in the target table are unaffected.

INSERT INTO SELECT Syntax

Copy all columns from one table to another table:

INSERT INTO table2
SELECT * FROM table1
WHERE condition;

Copy only some columns from one table into another table:

INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;

Demo Database

In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the “Customers” table:

Customer

And a selection from the “Suppliers” table:

Suppliers

MySQL INSERT INTO SELECT Examples

The following SQL statement copies “Suppliers” into “Customers” (the columns that are not filled with data, will contain NULL):

Example

INSERT INTO Customers (CustomerName, City, Country)
SELECT SupplierName, City, Country FROM Suppliers;

The following SQL statement copies “Suppliers” into “Customers” (fill all columns):

Example

INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
SELECT SupplierName, ContactName, Address, City, PostalCode, Country FROM Suppliers;

The following SQL statement copies only the German suppliers into “Customers”:

Example

INSERT INTO Customers (CustomerName, City, Country)
SELECT SupplierName, City, Country FROM Suppliers
WHERE Country='Germany';

Saturday, July 29, 2023

MySQL ANY and ALL Operators

 MySQL ANY and ALL Operators

The ANY and ALL operators allow you to perform a comparison between a single column value and a range of other values.

The ANY Operator

The ANY operator:

  • returns a boolean value as a result
  • returns TRUE if ANY of the subquery values meet the condition

ANY means that the condition will be true if the operation is true for any of the values in the range.

ANY Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name operator ANY
(SELECT column_name
FROM table_name
WHERE condition);

Note: The operator must be a standard comparison operator (=, <>, !=, >, >=, <, or <=).

The ALL Operator

The ALL operator:

  • returns a boolean value as a result
  • returns TRUE if ALL of the subquery values meet the condition
  • is used with SELECT, WHERE and HAVING statements

ALL means that the condition will be true only if the operation is true for all values in the range.

ALL Syntax With SELECT

SELECT ALL column_name(s)
FROM table_name
WHERE condition;

ALL Syntax With WHERE or HAVING

SELECT column_name(s)
FROM table_name
WHERE column_name operator ALL
(SELECT column_name
FROM table_name
WHERE condition);

Demo Database

Below is a selection from the “Products” table in the sample database:

Products

And a selection from the “OrderDetails” table:

OrderDetails

SQL ANY Examples

The following SQL statement lists the ProductName if it finds ANY records in the OrderDetails table has Quantity equal to 10 (this will return TRUE because the Quantity column has some values of 10):

Example

SELECT ProductName
FROM Products
WHERE ProductID = ANY
(SELECT ProductID
FROM OrderDetails
WHERE Quantity = 10);

SQL ALL Examples

The following SQL statement lists ALL the product names:

Example

SELECT ALL ProductName
FROM Products
WHERE TRUE;

The following SQL statement lists the ProductName if ALL the records in the OrderDetails table has Quantity equal to 10. This will of course return FALSE because the Quantity column has many different values (not only the value of 10):

Example

SELECT ProductName
FROM Products
WHERE ProductID = ALL
(SELECT ProductID
FROM OrderDetails
WHERE Quantity = 10);

Friday, July 28, 2023

MySQL EXISTS Operator

           MySQL EXISTS Operator

The EXISTS operator is used to test for the existence of any record in a subquery.

The EXISTS operator returns TRUE if the subquery returns one or more records.

EXISTS Syntax

SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);

Demo Database

Below is a selection from the “Products” table in the sample database:

Products

And a selection from the “Suppliers” table:

Suppliers

The following SQL statement returns TRUE and lists the suppliers with a product price less than 20:

Example

SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID = Suppliers.supplierID AND Price < 20);

Example — With SELECT Statement using NOT EXISTS

The MySQL EXISTS condition can also be combined with the NOT operator.

For example,

SELECT *
FROM Products
WHERE NOT EXISTS (SELECT *
FROM Suppliers
WHERE Products.SupplierID = Suppliers.SuppliersID);

Example — With DELETE Statement

The following is an example of a DELETE Statement that uses the MySQL EXISTS condition:

DELETE FROM Suppliers
WHERE EXISTS (SELECT *
FROM Products
WHERE Suppliers.SupplierID = Products.SupplierID);

Thursday, July 27, 2023

MySQL HAVING Clause

               MySQL HAVING Clause

The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions.

HAVING Syntax

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);

Demo Database

Below is a selection from the “Customers” table in the Northwind sample database:

Customers

MySQL HAVING Examples

The following SQL statement lists the number of customers in each country. Only include countries with more than 5 customers:

Example

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;

The following SQL statement lists the number of customers in each country, sorted high to low (Only include countries with more than 5 customers):

Example

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;

HAVING clause with COUNT() function

The HAVING clause can be used with the COUNT() function to filter groups based on the number of rows they contain.

Example

Following is the query, which would display a record where count of similar country is greater than or equal to 2.

SELECT Country
FROM CUSTOMERS
GROUP BY Country
HAVING COUNT(Country) >= 2
Employees

HAVING clause with MAX() function

We can also use the HAVING clause with MAX() function to filter groups based on the maximum value of a specified column.

Example

In here, we are trying to retrieve the Designation of the customers whose maximum salary is less than 1,00,000

SELECT Designation, MAX(salary) as max_salary
FROM Employees
GROUP BY Designation
HAVING MAX(salary) < 1,00,000

Building Static Website(part6) HTML Lists

  Building Static Website (part6) HTML Lists Today, let us add some lists to our detailed view section by using html lists. Lists: List is a...