Currently learning Data Science, here is what I've learnt so far. Pt.2
This time I learned about queries for data retrieval, feel free to read my notes and make any comments on tips, corrections or suggestions. Thx! :) Rename columns (AS) SELECT column_name AS 'column_rename' FROM table_name ; SELECT DISTINCT column_name FROM table_name ; DISTINCT is used to return unique values in the output. It filters out all duplicate values in the specified column(s). SELECT…
In the continuation of my journey to learn Data Science, I have delved into the intricacies of querying data. This time, my focus has been on retrieving information from databases. I have documented my findings and welcome any feedback or suggestions to enhance my understanding.
One aspect I explored was renaming columns using the AS operator. With the command SELECT column_name AS column_rename FROM table_name, I can assign a new name to a column in the output. This proves useful when presenting data in a more readable format.
To eliminate duplicate values, the DISTINCT keyword comes into play. Employing SELECT DISTINCT column_name FROM table_name ensures that only unique values are returned in the specified column(s), filtering out any repetitions.
The WHERE clause enables filtering of the result set based on specific conditions. By utilizing SELECT column_name FROM table WHERE condition, I can retrieve only the rows where the given condition is true. Additionally, the WHERE clause allows for more complex searches using comparison operators such as =, <, >, <=, >=, and <>.
When searching for specific patterns within a column, the LIKE operator proves invaluable. Within the WHERE clause, SELECT * FROM table_name WHERE column_name LIKE pattern enables me to locate rows where the column's value matches a particular pattern. The wildcard character % can be incorporated to match zero or more missing characters.
Handling unknown values is made possible through the use of NULL. The conditional operators IS NULL and IS NOT NULL allow me to filter out or include rows where the column's value is NULL.
The BETWEEN operator facilitates filtering within a specific range. By applying SELECT column_name FROM table_name WHERE other_column_name BETWEEN value1 AND value2, I can retrieve rows where the column's value falls between two specified values, whether they are numbers, text, or dates.
To combine multiple conditions in a WHERE clause, the AND operator is employed. By using SELECT column_name FROM table_name WHERE other_column_name1 BETWEEN value1 AND value2 AND other_column_name2 = value3, I can retrieve rows where all specified conditions are met.
The OR operator enables combining multiple conditions, where only one of the conditions needs to be true. By utilizing SELECT column_name FROM table_name WHERE other_column_name1 = value1 OR other_column_name2 = value2, I can retrieve rows where either condition is satisfied.
The ORDER BY clause allows for sorting the result set in a desired order. With SELECT column_name FROM table_name WHERE condition ORDER BY other_column_name DESC/ASC, I can arrange the output alphabetically or numerically in either descending or ascending order. However, it is important to note that ORDER BY always follows the WHERE clause, if it is present.
Furthermore, the LIMIT keyword determines the maximum number of rows in the result set. By incorporating SELECT column_name FROM table_name WHERE condition LIMIT x, I can restrict the output to a specified number of rows, such as the first 10.
Lastly, the CASE statement introduces conditional logic into the SELECT statement. By using SELECT column_name, CASE WHEN condition THEN output WHEN condition THEN output ELSE output END FROM table_name, I can create different outputs based on specified conditions, similar to an if-then logic structure.
These concepts form the foundation of querying data in a structured manner. As I continue my data science journey, I remain open to further insights and improvements from the community.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to