(Last Updated On: March 1, 2019)

How can I run SQL queries on CSV files?. So you have a good mastery of SQL and would like to use the same when working with CSV or TSV files?. To achieve this, we will use q which is a command line tool that allows for direct execution of SQL-like queries on CSVs files, TSVs, and other tabular text files.

Installing q on Linux / macOS / Windows

The first action of the business is to download and install q program in your system. The packages for Mac, Linux, and Windows are available on the Downloads page. Here I’ll show you how to do the installation on Debian based systems and Ubuntu.

Check for the latest release of q on Github releases page. As of this writing, this is 1.7.1.

Install q on Ubuntu / Debian

Download and install q on Debian / Ubuntu

export VER="1.7.1"
wget https://github.com/harelba/q/releases/download/${VER}/q-text-as-data_${VER}-2_all.deb

Then install the downloaded package.

sudo apt-get -y install python
sudo dpkg -i q-text-as-data_${VER}-2_all.deb

The version installed can be confirmed using the command:

$ q --version
q version 1.7.1
Copyright (C) 2012-2017 Harel Ben-Attia ([email protected], @harelba on twitter)
http://harelba.github.io/q/

Install q on CentOS / Fedora

For CentOS system, download and install the .rpm package.

export VER="1.7.1"
wget https://github.com/harelba/q/releases/download/${VER}/q-text-as-data-${VER}-1.noarch.rpm

Install downloaded file with rpm command.

sudo rpm -ivh q-text-as-data-${VER}-1.noarch.rpm

Install q on Arch Linux

For Arch Linux, you need to use AUR helper. Check our guide on yay – Best AUR Helper for Arch Linux / Manjaro. Once you have yay AUR helper installed, use it to get q.

yay -S --noconfirm --needed q

Package details can be obtained with:

Install q on Windows

If you’re running a Windows operating system, download the latest q exe file format and install it by double clicking the file.

Using q to run SQL queries directly on CSV or TSV file

Since CSV data will vary from person to person, I recommend you check q examples page to reference what fits your use case.

You can also work with data obtained from Linux command line.

$ cd /tmp
$ ls -l > tmpdata
$ q "SELECT COUNT(*) FROM tmpdata"
28
$ q "SELECT c3,COUNT(1) FROM tmpdata GROUP BY c3"
1
jmutai 27

Check for on examples page. Also check:

How to Convert ePub file to PDF Format on Linux CLI