Most voted "sql" questions
Structured Query Language (SQL) is a language to query databases. Questions should include examples of table code and structure. This tag refers to the default language, not to be used for questions about the implementation of specific DBMS (such as Mysql, Postgresql, Oracle, MS SQL Server). In this case use the specific DBMS tag. Answers to questions marked with ANSI SQL should use SQL as much as possible.
Learn more…6,771 questions
Sort by count of
-
2
votes1
answer25
viewsTest for the absence of duplicate tuples
I saw in a SQL book the following query: select T.course_id from course as T where unique (select R.course_id from section as R where T.course_id = R.course_id and R.year = 2009) I am unable to…
-
2
votes1
answer217
viewsSQL JOIN multiple tables
I have 4 tables that I need to extract the results, but I always get duplicate records, someone can help? tabclientes: codcli nome 1 A 2 B 3 C tabrepre: codrepre nome 1 AA 2 AB 3 AC tabvendedor:…
-
2
votes2
answers2513
viewsSQL - How to Get Last Day of the Previous Month Dynamically In This Situation
Good, how do I make to obtain the last day of the previous month dynamically (associated with the current date); I have a spine which is associated with the date on Milliseconds 'Wo.CREATEDTIME' -…
-
2
votes1
answer67
viewsWhat is the best way to update multiple tables using a Trigger?
I have a Trigger for After Insert notes After delete notes After update notes And I need to update several tables like, sales, sales_seller. I tested using a cursor for each table, at first it…
-
2
votes3
answers64
viewsWhy using 'IN" to filter records along with a subselect returns records that have, in certain columns, equal values
I was studying and I came across this, I know IN is like the OR, but what I didn’t understand was this command: SELECT * FROM Customers WHERE Country IN (SELECT Country FROM Suppliers); The return…
-
2
votes1
answer1137
viewsHow do auto increment in Mysql without declaring columns in INSERT?
I noticed that when I have a column with auto_increment, I have to declare the columns on INSERT. For example, I created this test table: create table teste( id int auto_increment, nome varchar(100)…
-
2
votes1
answer288
viewsQuery two or more words in a string
I need to make mine select bring only records whose description has the words "RACAO" and "ROYAL". I’m using Regex to make it easier, but I don’t know why you’re bringing in records that have…
-
2
votes1
answer410
viewsNode.JS and Express Insert with problem
I am new as Node.JS Express programmer with Mysql, I created the implementation to save, but he create the registry with null in the table, see how I performed the test in Postman; This is my…
-
2
votes2
answers269
viewsUpdate tables using Trigger from a main table
I have the following structure: A main table X, where the "header" of the sale is stored. A Y table, where the sale items are stored. A Z table, where some sales data will be saved, from the…
-
2
votes1
answer202
viewsHelp with GROUP_CONCAT (Mysql)
I’d like to use a grupo_concat for filename and answer but not to repeat the values of filename and answer Follows sqlfiddle: http://sqlfiddle.com/#! 9/f32d78/1/0 It is a question that has 2 images…
-
2
votes0
answers170
viewsConvert sql into eloquent
How can I convert this code sql in the eloquent? SELECT CONCAT(FLOOR(sum(diferenca)/60),'h',MOD(sum(diferenca),60),'m') as tempo FROM (SELECT TIMESTAMPDIFF(MINUTE, m1.created_at, min(m2.created_at))…
-
2
votes1
answer25
viewsWhat best practice to create a user table?
I’m creating a web system in PHP, and I’m in doubt about how best to create the table that will receive user information (Speed, organization and etc). Beyond the nome, email and senha, need to…
-
2
votes3
answers121
viewsSelect the first result of each conversation
How can I select the first line of each conversation from a specific user where the to_id = 1. The big problem is when the first message exchanged from the conversation does not have the top_id = 1…
-
2
votes1
answer664
viewsProblem in a simple Inner Join Update
I’m running a simple update on my base, however set which I am giving, is being replicated for all rows of the table. Follow the script executed: update reference_vendor set internalid =…
-
2
votes2
answers436
viewsSQL parameter in PDO PHP does not work
The following code returns 11 records from the database: $dbh = new PDO('sqlite:db_coleta.sqlite3'); $sth = $dbh->prepare('SELECT * FROM ROTA_VIEW WHERE usuario_id = 1 AND 0 = 0');…
-
2
votes1
answer92
viewsAverage and count for each SQL product
I’m trying to update a table in my database into two new columns, one that brings the average of the ratings and one that brings the number of ratings. These data are obtained through two tables,…
-
2
votes1
answer321
viewsField condition with subquery
Example of select: SELECT campo1, (ROUND(((t3.quantidade * t2.distancia * (SELECT TOP 1 valor FROM frete WHERE datainicio <= t1.dataemissao AND tipo = t4.tipo ORDER BY DATAINICIO DESC)) -…
-
2
votes2
answers406
viewsSelect dates that are not in the table
I need to compile a report for a client with the billing for each day of a given month. Until then beauty, the problem is that now he wants even the days that have no sale, are shown in the table…
-
2
votes4
answers1185
viewsChange only the day of registration
Scenario (example): I have the following table: ID | TIPO | DATAINCLUSAO 1 | 10 | 21/07/2018 09:34:51 2 | 10 | 11/07/2018 11:15:25 3 | 11 | 23/07/2018 01:52:31 4 | 11 | 04/07/2018 23:24:52 5 | 12 |…
-
2
votes1
answer60
viewsReturn multiple keys using REGEXP_SUBSTR
I am trying to read occurrences of text records based on an occurrence. By breaking my head I got the following results: For example I want to take the second name, using as a basis the key Empr.:…
-
2
votes2
answers62
viewsChange the end of the email domain to a random scan
Hello! I’m looking for a way to mask emails in the database, but in a way that I can reverse them without much difficulty, in case I need the real email. My idea is to create a Rigger AFTER INSERT…
-
2
votes2
answers271
viewsDapper requires writing SQL code, why?
I’m looking at the following link that uses Dapper: Getting Started With Postgresql Using Dapper In . NET Core and my question is, when I do Java I don’t need to write SQL code as this example here:…
-
2
votes2
answers1445
viewsUPSERT or UPDATE-INSERT?
Across of that question felt the need to research and understand a little about the UPSERT. I was wondering What blessed command is that? I’ve never seen.. I found some information but it is not…
-
2
votes1
answer94
viewsFirst and Last record including night
I have the following problem I have the data that way: codigo data_hora nome_ajudante -------+----------------+-------------- 1000004 2018-08-22 11:11 Carlos Eduardo 1000004 2018-08-22 11:43 Carlos…
-
2
votes1
answer528
viewsError: SQL Server String or Binary data would be truncated
I have the variable @numero1 which brings the following result: 020000000000720000018 But at the time of update gives error: SQL Server String or Binary data would be truncated declare @g1…
-
2
votes1
answer7520
viewsQt SQL - ERROR (42601): syntax error at or near
I am currently developing a program to assist in the management of a barracks in my city, I am using Qt to create the graphical interface and other libraries needed as the manipulation of databases,…
-
2
votes3
answers89
viewsproblem with select case
I am selecting with case comparing different status of the same field and counting them in separate columns. The problem is that the first column is NULL until the list of the second finishes…
-
2
votes1
answer73
viewsRelate groups among Researchers with at least 2 common areas in MYSQL
I need to group all Researcher-type users (idUsuarioType = 3) who have 2 areas in common (idAreaAtuation) Example: Maria, Pedro, José and Roberto have the areas Environment and Ecology in common.…
-
2
votes2
answers113
viewsInner with 3 PHP/SQL tables
I have 3 tables and I want to compare their value, I can do with 2 using the JOIN but when I put JOIN in the third point php doesn’t work. Code in PHP: $agora = "select * from usuario u join…
-
2
votes0
answers42
viewsDifficulty with JPA annotations for entity relationships
I have the following relationships: Categoriapeca, Subcategoriapeca and Peca. One Categoriapeca may have several Subcategoriapeca, and a Subcategoriapeca may have several Categoriapeca, a piece can…
-
2
votes1
answer27
viewsPHP Receive the ID of a value via select option
Oops, guys, I’m developing a code and I’ve been at this part for days; I need to get a value to register it as a table ID. This part of the code works perfectly. It demonstrates a dropdown of what I…
-
2
votes2
answers1897
viewsHow to do a subquery in eloquent Laravel with WHERE
Hello, I am using Laravel 5.6 and I am creating a query using eloquent Laravel, however I am having difficulty creating a subquery what I want to create is simple in SQL server: ,DT_ALTA = (SELECT…
-
2
votes2
answers1138
viewsSQL Syntax Error Exception: ORA-00928: SELECT keyword not found
I have this message while trying to execute a Stored Procedure created on the Oracle: java.sql.Sqlsyntaxerrorexception: ORA-00928: SELECT keyword not found at…
-
2
votes0
answers59
viewsDead Lock with SQL SERVER PROCEDURE
I need to generate a table of games for four groups with 5 teams, for each day a total of games must be held, at the time that the PROCEDURE which executes the insertion starts is called the…
-
2
votes3
answers141
viewsShouldn’t Python none be semantically equal to SQL Null?
That makes sense: >>> if True==True: ... print ('True') ... True That too: >>> if False==False: ... print ('True') ... True But that for me should return nothing, but returns…
-
2
votes2
answers44
viewsDoubt in Postgresql function
I am doing a function in Postgresql and I want to use it to check if a table already exists or not in a database and, if not, I want to create a table. The problem is in the function declaration,…
-
2
votes1
answer97
viewsAdd another sum result
I’m trying to make a sum of the result of another operation. Below is the code I’m trying to select distinct (SELECT(SUM(CAST(ROUND(ppre.Valor_Custo, 2) as decimal(18,2))) )) * (select…
-
2
votes1
answer630
viewsNodejs query in SQL Server
Hello, guys I need to make a query filtered for days in SQL in a Nodejs application, but during the POST that’s when I filter the Nodejs accuses me of a value conversion error. When running get I…
-
2
votes1
answer77
viewsCalculate the time difference without giving the date
I’m trying to figure out the difference between two hours. However, I would not like to give the dates. I would like to know for example that between 23:00 and 01:00 it was 120 minutes. What I tried…
-
2
votes2
answers55
viewsDoubt with group by - sql
I have these tables: CREATE TABLE empresa ( id_empresa INT, nome_empresa VARCHAR(40), razao_social VARCHAR(40), PRIMARY KEY ( id_empresa ) ); CREATE TABLE departamento ( id_departamento INT,…
-
2
votes1
answer693
viewsFunction to return months of year sql server
I’m new in sql server and would like if possible a help with an exercise I’m doing. I have a query that she returns me the region of a resale, the name of the resale, the cnpj, the registration date…
-
2
votes0
answers32
viewsSpecial character query in SQL
I need to make a query in a table of emails, and I need to return only emails that have special characters. Can you help me with the script, I tried to use this code: Select * from tabela Where…
-
2
votes1
answer873
viewsError executing report in SQL Server Reporting Services
The following error is occurring while running a Reporting Services report: "Report processing error. (rsProcessingAborted) Not possible to create a connection to the data source 'Production'.…
-
2
votes2
answers22
viewsDoubts with PIVOT on Oracle
Good afternoon, I’m having trouble setting up a Pivot on the Oracle. My query returns the categories in general, so I’m using the pivot to count the number of categories. pivot( count(CAT) for CAT…
-
2
votes1
answer201
viewsShow NULL SQL values
Good evening, I’m new to SQL and I have a question. I have the following database: My goal is to present the name of the employees, their function and the name of the department where they work…
-
2
votes1
answer75
viewsBring the sum of IDS and data into two tables using SQL
I need to make an SQL to bring me the data of: `DATA | QTDE IDTa | QTDE IDTb | VALORa | VALORb | Saldo (diferença de VALORa - VALORb) ` However I am not able to bring the data correctly with this…
-
2
votes1
answer79
viewsSum subquerys values separately
I have a problem in this query below: select sum(tb1.l1) * 0.3, sum(tb2.l2) * 0.3 from ( select setor, total_geral as l1 from mobile.auditoria where month(data) = month(now()) and year(data) =…
-
2
votes1
answer160
viewsError when using ISDATE in CASE WHEN
Hello, everybody. I am running the query below and is showing the error: "Operand type Clash: int is incompatible with date". My intention in performing this query is to validate whether the field…
-
2
votes2
answers515
viewsINNER JOIN in 4 tables
I have 4 tables: TB_OS_MANUTENCAO: TB_OS_ELETRONICA: TB_OS_MECANICA: E TB_OS_INFORMATICA: as shown in the images, all tables have a common field, calledid_, and also with a common value, 8. This…
-
2
votes2
answers51
viewsQuery does not work inside php
I made a query with a Join to get information from the database, I tested it in mysql and it worked perfectly, the problem is that it does not work in php, not from the error in the execution but…