Yash Sanghvi has Published 192 Articles

Extract day, hour, minute, etc. from a datetime column in PostgreSQL?

Yash Sanghvi

Yash Sanghvi

Updated on 02-Feb-2021 13:08:17

1K+ Views

Let us create a new table containing a single timestamp column −CREATE TABLE timestamp_test(    ts timestamp );Now let us populate it with some data −INSERT INTO timestamp_test(ts) VALUES(current_timestamp), (current_timestamp+interval '5 days'), (current_timestamp-interval '18 hours'), (current_timestamp+interval '1 year'), (current_timestamp+interval '3 minutes'), (current_timestamp-interval '6 years');If you query the table (SELECT * ... Read More

How to combine different columns of a table to yield a single column in query output in PostgreSQL?

Yash Sanghvi

Yash Sanghvi

Updated on 02-Feb-2021 13:02:48

2K+ Views

Suppose you have a table user_info that contains the state and district of different users. An example is given below −namedistrictstateAnilMumbaiMaharashtraJoyJhalawarRajasthanRonPuneMaharashtraReenaMeerutUttar PradeshNow, if you want to combine the state and district in a single field called location, this is how you should be able to do it −SELECT name, district ... Read More

How to look for partial string matches in queries in PostgreSQL?

Yash Sanghvi

Yash Sanghvi

Updated on 02-Feb-2021 13:00:52

5K+ Views

Suppose you have a table user_info containing the names of users and their addresses. An example is given below −nameaddressAnilAndheri, Mumbai, MaharashtraJoyChandni Chowk, DelhiRonBandra, Mumbai, MaharashtraReenaOld Airport Road, Bengaluru, KarnatakaNow, if you want to just extract the information of users who stay in Mumbai, you can do that using the ... Read More

How to apply DISTINCT constraint on select columns in queries in PostgreSQL?

Yash Sanghvi

Yash Sanghvi

Updated on 02-Feb-2021 12:57:26

212 Views

Suppose you have a table exam_scores containing 5 columns. An example is given below with some dummy data.nameroll_nosubjecttotal_marksmarks_obtainedAnil1English10056Anil1Math10065Anil1Science10045Roy2English10078Roy2Math10098Roy2Science10067Now, one student could have sat for exams of multiple subjects, and therefore, there are multiple rows for 1 student. If you wish to find out the total number of students in the ... Read More

CASE WHEN in PostgreSQL?

Yash Sanghvi

Yash Sanghvi

Updated on 02-Feb-2021 12:55:49

2K+ Views

If you are a programmer, you may be very familiar with IF-ELSE statements. The equivalent in PostgreSQL is CASE WHEN.Let’s understand with an example. If you have table marks containing percentage marks of a student, and you want to find out whether the students have passed or failed. An example ... Read More

How to get current timestamp and relative timestamps in PostgreSQL?

Yash Sanghvi

Yash Sanghvi

Updated on 02-Feb-2021 12:54:56

3K+ Views

Quite often, you need the current timestamp in PostgreSQL. You do that as follows −SELECT current_timestampIt will output the current time. The output will look like the following −2021-01-30 15:52:14.738867+00Now, what if you want the relative time instead of the current time? For example, if you want the time corresponding ... Read More

How to insert values from one table into another in PostgreSQL?

Yash Sanghvi

Yash Sanghvi

Updated on 02-Feb-2021 12:30:41

2K+ Views

Suppose you have two tables: marks and student_info. Examples are given below for the two respectivelynameroll_noperc_marksAniket1224Siddhi4565Yash2642Isha5687nameroll_noagegenderAniket1226MIsha5625FSiddhi4523FYash2625MNow, suppose your manager at work looks at the two tables and tells you, “Why do we have two tables? Simplify things, shift everything to one table!”So you decide to add the perc_marks column to ... Read More

How to define and query json columns in PostgreSQL?

Yash Sanghvi

Yash Sanghvi

Updated on 02-Feb-2021 12:22:41

1K+ Views

The ability to define JSON columns in PostgreSQL makes it very powerful and helps PostgreSQL users experience the best of both worlds: SQL and NoSQL.Creating JSON columns is quite straightforward. You just have to create/ define it like any other column and use the data type as JSON.Let us create ... Read More

How to change the type of a column in PostgreSQL?

Yash Sanghvi

Yash Sanghvi

Updated on 02-Feb-2021 12:11:32

5K+ Views

In order to change the type of a column, the syntax isALTER TABLE table_name ALTER COLUMN column_name TYPE new_data_type USING expression;Please note that altering the type of a column is not recommended generally, especially if your table has a lot of entries already.The USING part of the syntax is optional. ... Read More

How to add column to an existing table in PostgreSQL?

Yash Sanghvi

Yash Sanghvi

Updated on 02-Feb-2021 12:07:55

1K+ Views

The syntax to add a new column to an existing table is quite straightforward.ALTER TABLE table_name ADD COLUMN column_name column_type column_constraint;Say you have existing table marks. An example is given below −serial_nonameroll_nomarks_obtainedperc_marksmax_marksdate_of_entry1Yash2642421002021-01-302Isha5617587.52002021-01-30Now, suppose you want to add a column named subject. You can do that using −ALTER TABLE marks ADD ... Read More

Advertisements