Thursday, January 7, 2021

Cheatsheets

 Skip to Content

List of SQL Commands

Glossary of commonly used SQL commands.

Update: Cheatsheets BETA is here!


Background

SQL, Structured Query Language, is a programming language designed to manage data stored in relational databases. SQL operates through simple, declarative statements. This keeps data accurate and secure, and it helps maintain the integrity of databases, regardless of size.

Here’s an appendix of commonly used commands.

Commands

ALTER TABLE

ALTER TABLE table_name
ADD column_name datatype;

ALTER TABLE lets you add columns to a table in a database.

AND

SELECT column_name(s)
FROM table_name
WHERE column_1 = value_1
  AND column_2 = value_2;

AND is an operator that combines two conditions. Both conditions must be true for the row to be included in the result set.

AS

SELECT column_name AS 'Alias'
FROM table_name;

AS is a keyword in SQL that allows you to rename a column or table using an alias.

AVG()

SELECT AVG(column_name)
FROM table_name;

AVG() is an aggregate function that returns the average value for a numeric column.

BETWEEN

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value_1 AND value_2;

The BETWEEN operator is used to filter the result set within a certain range. The values can be numbers, text or dates.

CASE

SELECT column_name,
  CASE
    WHEN condition THEN 'Result_1'
    WHEN condition THEN 'Result_2'
    ELSE 'Result_3'
  END
FROM table_name;

CASE statements are used to create different outputs (usually in the SELECT statement). It is SQL’s way of handling if-then logic.

COUNT()

SELECT COUNT(column_name)
FROM table_name;

COUNT() is a function that takes the name of a column as an argument and counts the number of rows where the column is not NULL.

CREATE TABLE

CREATE TABLE table_name (
  column_1 datatype,
  column_2 datatype,
  column_3 datatype
);

CREATE TABLE creates a new table in the database. It allows you to specify the name of the table and the name of each column in the table.

DELETE

DELETE FROM table_name
WHERE some_column = some_value;

DELETE statements are used to remove rows from a table.

GROUP BY

SELECT column_name, COUNT(*)
FROM table_name
GROUP BY column_name;

GROUP BY is a clause in SQL that is only used with aggregate functions. It is used in collaboration with the SELECT statement to arrange identical data into groups.

HAVING

SELECT column_name, COUNT(*)
FROM table_name
GROUP BY column_name
HAVING COUNT(*) > value;

HAVING was added to SQL because the WHERE keyword could not be used with aggregate functions.

INNER JOIN

SELECT column_name(s)
FROM table_1
JOIN table_2
  ON table_1.column_name = table_2.column_name;

An inner join will combine rows from different tables if the join condition is true.

INSERT

INSERT INTO table_name (column_1, column_2, column_3)
VALUES (value_1, 'value_2', value_3);

INSERT statements are used to add a new row to a table.

IS NULL / IS NOT NULL

SELECT column_name(s)
FROM table_name
WHERE column_name IS NULL;

IS NULL and IS NOT NULL are operators used with the WHERE clause to test for empty values.

LIKE

SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;

LIKE is a special operator used with the WHERE clause to search for a specific pattern in a column.

LIMIT

SELECT column_name(s)
FROM table_name
LIMIT number;

LIMIT is a clause that lets you specify the maximum number of rows the result set will have.

MAX()

SELECT MAX(column_name)
FROM table_name;

MAX() is a function that takes the name of a column as an argument and returns the largest value in that column.

MIN()

SELECT MIN(column_name)
FROM table_name;

MIN() is a function that takes the name of a column as an argument and returns the smallest value in that column.

OR

SELECT column_name
FROM table_name
WHERE column_name = value_1
   OR column_name = value_2;

OR is an operator that filters the result set to only include rows where either condition is true.

ORDER BY

SELECT column_name
FROM table_name
ORDER BY column_name ASC | DESC;

ORDER BY is a clause that indicates you want to sort the result set by a particular column either alphabetically or numerically.

OUTER JOIN

SELECT column_name(s)
FROM table_1
LEFT JOIN table_2
  ON table_1.column_name = table_2.column_name;

An outer join will combine rows from different tables even if the join condition is not met. Every row in the left table is returned in the result set, and if the join condition is not met, then NULL values are used to fill in the columns from the right table.

ROUND()

SELECT ROUND(column_name, integer)
FROM table_name;

ROUND() is a function that takes a column name and an integer as arguments. It rounds the values in the column to the number of decimal places specified by the integer.

SELECT

SELECT column_name
FROM table_name;

SELECT statements are used to fetch data from a database. Every query will begin with SELECT.

SELECT DISTINCT

SELECT DISTINCT column_name
FROM table_name;

SELECT DISTINCT specifies that the statement is going to be a query that returns unique values in the specified column(s).

SUM

SELECT SUM(column_name)
FROM table_name;

SUM() is a function that takes the name of a column as an argument and returns the sum of all the values in that column.

UPDATE

UPDATE table_name
SET some_column = some_value
WHERE some_column = some_value;

UPDATE statements allow you to edit rows in a table.

WHERE

SELECT column_name(s)
FROM table_name
WHERE column_name operator value;

WHERE is a clause that indicates you want to filter the result set to include only rows where the following condition is true.

WITH

WITH temporary_name AS (
   SELECT *
   FROM table_name)
SELECT *
FROM temporary_name
WHERE column_name operator value;

WITH clause lets you store the result of a query in a temporary table using an alias. You can also define multiple temporary tables using a comma and with one instance of the WITH keyword.

The WITH clause is also known as common table expression (CTE) and subquery factoring.

v

With Statement in SQL


-- With statement use in incorporating playcount and join tables with songs.

With play_count AS (SELECT song_id,
   COUNT(*) AS 'times_played'
FROM plays
GROUP BY song_id)
Select songs.title, songs.artist, play_count.times_played from songs join play_count on play_count.song_id = songs.id;


--Delete when students cannot enrolled 2 courses at the same time.
DELETE FROM statistics_students
WHERE id in (
  SELECT id
  FROM history_students);

--Subquery sample to find the number of students from drama = the same grade as a particular student with the id =20

select * from drama_students where grade = (select grade from band_students where id=20)


-- To find student details from band who are not in drama club.
select first_name, last_name from band_students where id NOT IN
(select id from drama_students);

-- to find out the grades that are in both band and drama club.
select grade from band_students where exists (select grade from drama_students);

-- to find the top 2 , always think of order in desc + limit, mssql can use top 2
select * from cars order bytrip_completed limit 2;

Wednesday, January 6, 2021

Priority of order in Syntax for where, group by, having, order by

There are particular sequence in sql syntax as per follows: 


SELECT ...

WHERE..

GROUP BY..

HAVING

ORDER BY...DESC

Example 

SELECT strftime('%H',timestamp) as Hours , round(AVG(score),0), COUNT(*) as Count_of_Stories FROM hacker_news 
where timestamp is NOT NULL
GROUP BY 1 
ORDER BY 1 

Tuesday, January 5, 2021

UNION VERSUS UNION ALL IN MSSQL

 DIFFERENCE BETWEEN UNION AND UNION ALL


Union All shows all 9 fruits while Union will filter out repeated fruit names in both tables. 










Wednesday, October 14, 2020

Shubby Sweets








Shubby Sweets

Located near bedok chai chee 42

If you are thinking of a sweet treat, then do check out the shubby sweets.

It is a neighbourhood shop and i often check out that area when visiting my sister. 

It eels very nice brownie and definitely sweet to the fullest. Therefore, if u need occasional treats for yourself or your family, here is a pastery shop to recommend. The price ranges $7.50 -$10 and u get 4-5 pastries. It can be a little “jelak” in Malay for some, meaning excessively sweet. But the brownie is in the right small portion to place all or at least half in your mouth. Prepare lotsa beverages, preferably non-sweetened ones to go with it. 

The operating hours are Wednesday to Sunday, 2pm-7pm or until sold out. Monday to Tuesday are closed.

 I believe it is halal.. so muslims should try out too! 


 

Thursday, October 8, 2020

Definition of Fashion


 What is Fashion?

Fashion in my opinion is an expression of an individual's preference in apparel/dressing. It can also be an art and serves as a testimonial of time.

It is the result of the evolution from primitive needs for protection against weather conditions to a significance of wealth and social status to just a individual's freedom expression of character and beauty standards.

In Singapore, we do not enjoy 4 seasons, only summer or closer to spring (in air-conditioned environment) This limits our fashion variety but also wastage of resources. In view of covid-19, the needs for winter clothing are made redundant.

Singapore is never a place for fashion runway as we are mainly under influence of other countries Korea, Japan, Hong Kong, UK and US. They influence in the form of social media, celebrity like Korean Girls/Boy Band. We do not really have designs that can be called "from Singapore". However, we do have an evergreen culture in wearing T-shirts and Jeans (shorts). It has never gone out-of-fashion as per say.

One of the fashion founder, Coco Chanel once said " Dress Shabbily and they remember the dress. Dress impeccably, they remember the woman". How did Coco Chanel becomes so popular? in my opinion, she is a fashion statement on her own and she has the network to display her works. In today's world, we have no issue on platform in view of internet. We have issue in accessing the rich's network and gain their appeal in our work. What motivates the rich in buying our stuff? Probably a huge differentiating element or a statement of social status. Just like the reason why many of us like to buy LV, Dior, Celine, YSL, Hermes or Chloe. Why the general masses prefer Coach, Kate Spade or Agnes B? Both brands appeal to different audience with differing budget for fashion.

In Singapore, I would say most Singaporean are still in pursue of fashion but still in the shadow of the designers in other countries. We have not walked out and carve a path on our own in the fashion space. Probably there are already Singaporean-born designers, e.g. Ashley Islam (https://youtu.be/SWMKt1IwjOM) gaining some recognition in the international platform. However, we need more people like him. We need his apparel to gain fame beyond as a designer but also apparel chains like Zara and Mango so Singaporeans are able to wear the prideful brand made from our homegrown talent.

In my opinion, every expression of fashion should be respected but may differ in popularity over time. 
Popularity would usually center around a touching story. Our world has been greatly exposed to different stories. The design may need to be uniquely refreshing, never seen before in order to be impeccable in the current age and time. The design may need to draw inspirations from familiar items in our memory lane. 

The next big question is how to achieve above. There are various strategies. Once is to design and keep designing non-stop and volume-based. Hopefully one will takes the liking of a person. Another strategy is to design one and tell a story on it, hoping to touch the heart that one customer to buy into your designs. The next one is to gain access to the right platform and audience. To show them the designs and increase the chances. Finally, leave the design in the hands on the individuals. Since it is the expression of the individual, the best designer are the individuals themselves. We facilitate the right tools and empower the individuals to do customization. What is more convincing and touching story than their own? A custom-made design on T-shirt or bag for their special one. The time and effort spent what made the design special and memorable. 

I personally employed all the strategies above. Personally I am not designed trained. To contribute in the space would require leveraging. Therefore, I thought of applique. It offers a lot of flexibility and empowers a person to apply design to almost everything they own/wear i.e. bags, t-shirts, hair accessories, sneakers, heels and etc. 

Appliques fulfil the idea of empowerment. Especially the expression of individual's perception of beauty are more prevalent in the current era.