SQL

DELETE based on a JOIN condition

You may wish to delete records in one table based on values in another table. Since you can't list more than one table in the FROM clause when you are performing a delete, you can use the EXISTS clause.

For example:

DELETE FROM suppliers
    WHERE EXISTS
      ( select customers.name
         from customers
         where customers.customer_id = suppliers.supplier_id
         and customers.customer_name = 'IBM' );

Calculating Percentage of Total in SQL

Suppose we have the following table which consists of two columns - a user and his favorite album.

mysql> select * from albums_votes;
+-----------+----------+
| user      | album_id |
+-----------+----------+
|      1000 |        2 |
|      2000 |        3 |
|      3000 |        1 |
|      4000 |        3 |
|      5000 |        3 |
|      6000 |        3 |
| 531514255 |        1 |
| 686711159 |        2 |
| 618496371 |        2 |
| 705891065 |        2 |
+-----------+----------+
10 rows in set (0.00 sec)

Syndicate content