JOIN clause in SQL

The JOIN clause is extremely important in SQL language. The JOIN clause joins rows from two or more tables. That being said, let’s move straight to the examples and see what exactly the JOIN clause does!

Note:
You need my MySQL installed on your local machine.
You need Chinook database as well since the examples are based on Chinook (see this link for getting started http://dan.saigon.ro/mysql-and-sql-getting-started/).

As usual, start the server, connect to the server, display the databases, select Chinook, and show its tables.

service mysql start
mysql -u root -p

mysql> show databases;
mysql> use Chinook;
mysql> show tables;

Continue reading

Selecting data with SQL | SELECT statement and WHERE clause in SQL

In this post I am going to take a look at the SELECT statement for selecting data and the WHERE clause for filtering the results.

Before everything else, we have to start the MySQL server, connect to it, and select a database. You can do that by running the following commands:

To start the MySQL server:

service mysql start

To connect to the MySQL server:

mysql -u root -p

To select the Chinook database:

use Chinook;

Ok, we selected the Chinook database. Since it is made of tables we want now to see the tables.

Continue reading

MySQL and SQL – Getting started

First of all, to avoid any confusion, SQL it is the standard language for relational database management systems and MySQL is an open-source relational database management system. So, MySQL is a database and SQL is a language.

In order to use SQL language you have to install a relational database. One of the most popular relational databases is MySQL. Let’s install it!

I am on Linux and I installed MySQL via terminal by running the command:

sudo apt install mysql-client-core-5.7

Continue reading

How to solve a string “splosion” problem in Python using for loop

First of all, what is a string “splosion”. Let’s consider a non-empty string, “hello” for example. You will need to return a string like “hhehelhellhello”.

def stringSplosion(initial_string):
	resulted_string = ""
	for s in range(len(initial_string)):
		resulted_string = resulted_string + initial_string[:s+1]
	return resulted_string

So, we declare a function called stringSplosion which takes in an argument, the string initial_string.
Within the function we create an empty string which is going to be our result.
Now, we start iterating over the initial string. Every time we iterate we add the characters from the beginning of the string up to the s plus 1.

Continue reading

06 – Form filters in jQuery

Many times you need to select elements that are part of a form. For example you might want to select all check-boxes that are selected.

You could match by attribute $('input[type="checkbox"][checked]'); but that will match only the initial state (specified in HTML markup).

What you want to do is to check its state in real time. For that you have to use the CSS pseudo-class :checked. This pseudo-class matches all the elements that are in the checked state. So, you should write something like shown below:

$('input[type="checkbox"]:checked');

You may also want to combine the custom jQuery selector :checkbox with the CSS pseudo-class :checked to create a very powerful custom filter.

$('input:checkbox:checked');

Continue reading

How to open, read, and write text files in Python

Let’s say that you need to open, read, and write text files in Python. First you can use the open() built-in function to open the file. Once the file is opened you can use the two built-in methods read() and write() to read from the file and to write to the file.

>>> opened_file = open('file.txt')
>>> the_file_content = opened_file.read()
>>> the_file_content
'Hi there! I am a .txt file!'

The opened_file is a file descriptor object. In the above example, the read() method reads the entire content of the file. If you wish, you can read just a portion of the file, as is shown below:

Continue reading

05 – Selecting elements using child filters in jQuery

You can select elements in jQuery using CSS3 child pseudo-classes based on their position inside a parent element.

A few examples:

  • The ul li:last-child selects the last <li> of each <ul> element.
  • The div p:nth-child(2) retrieves all <p> elements inside a <div> that are the second child.
  • The div .main-div:nth-of-type(3) retrieves all the third elements having class main-div inside a <div>.

Note:

  • The :nth-child() counts all children elements regardless of their type and the index starts from 1 instead of 0.
  • The :eq() selects only the elements corresponding to the selector attached to the pseudo-class, regardless of how many siblings they have before them and the index starts from 0.

Continue reading

How to copy files using shutil module in Python

The shutil module contains many functions but two of them can be used to copy files from one location to another. To demonstrate this, I created two text files as shown in the image below:

copy files with shutil

You can copy the content of the file1.txt to the file2.txt. This way, both files will have the same content but they will still have their original names.

>>> import shutil
>>> new_file_path = shutil.copy('file1.txt', 'file2.txt')

Continue reading

How to create classes of iterators using itertools module in Python

The itertools module provides iterators that you can use in your projects. One of the commonly used method is accumulate.

>>> import itertools
>>> accumulator = itertools.accumulate(range(10))
>>> next(accumulator)
0
>>> next(accumulator)
1
>>> next(accumulator)
3
>>> next(accumulator)
6
>>> next(accumulator)
10
>>> next(accumulator)
15

Continue reading