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
-
5
votes2
answers86
viewsHow does ORDER BY draw columns in case of a repeated value?
Suppose I make one SELECT sort of like this: SELECT * FROM "users" ORDER BY "createdAt"; In this context, if I have data that has the same value in the field createdAt, how SQL will sort these…
-
5
votes1
answer58
viewsWhat is the $$ function in posntegresql functions?
Hello, I’m a beginner in postegrsql and I have the following question: When we create functions in postgresql the following logic is usually used CREATE FUNCTION func() RETURNS ret AS $$ BEGIN ...…
-
5
votes2
answers121
viewsPlace row data in columns
SELECT datacc, horacc FROM R070ACC WITH (NOLOCK) WHERE numcad = '2676' AND DATACC between ('2018-16-04') and ('2018-15-05') and oriacc = 'E' In the first column appears the day of the month repeated…
-
5
votes1
answer101
viewsPersist data in related tables (1 x N)
I have these 2 related classes, and the requested class has a Collection of "Itenspedidos"... The Request class: public class PedidoDTO { public int pedidoID { get; set; } public int codigo { get;…
-
5
votes1
answer322
viewsConcatenate columns into rows
I’m trying to take several columns and turn them into a single row, but without success. That’s the select I’m trying to make: SELECT (`dias2produto`+`dias4produto`) as 'Até 4 dias',…
-
5
votes1
answer41
viewsSelect by days of the month returning 0 when there is no record of that day
I have a company call database, for example let’s say the table has only: -- TABELA CHAMADO -- id (id do chamado) inicio (data de inicio do chamado) id_criador (id do usuário que abriu o chamado)…
-
5
votes1
answer3138
viewsWhy when using ON DUPLICATE KEY UPDATE or REPLACE, do we have change in 2 lines?
Example When executing any of the 2 commands, the message is returned: 2 Row(s) affected Query: ON DUPLICATE KEY UPDATE: INSERT INTO `banco`.`tabela` (`id`, `resumo`, `descricao`, `grupo`,…
-
5
votes2
answers76
viewsSQL SERVER Counting the data of a record
I came across the following situation: The table below is a Checklist and has a column of covenants, the others are questions answered with yes(1), no(0) and semPreenchimento(null or 9). I wanted to…
-
5
votes2
answers255
viewsReturn the highest value between columns and the row ID of that highest value
Let’s say I have two columns in my table, column A and B. +-----+-----+-----+ | ID | A | B | +-----+-----+-----+ | 1 | 500 | 681 | +-----+-----+-----+ | 2 | 980 | 101 | +-----+-----+-----+ | 3 | 110…
-
5
votes3
answers141
viewsResult range
I have a table where records are saved every 3 seconds 24 hours a day Table: myTable dado1 dado2 dado3 data "135,05" "134,69" "135,10" "2018-11-13 15:38:16" "132,00" "131,91" "132,61" "2018-11-13…
-
5
votes3
answers147
viewsJoin between banks
Staff I am running the command below but I haven’t gotten the desired result. USE banco1 SELECT * FROM sistema.tb_menus_perfil_params a LEFT JOIN banco2.sistema.tb_menus_perfil_params b ON a.pm_id =…
-
5
votes1
answer4672
viewsHow to use SQL "LIKE" in Sequelize?
I am developing using sequelize but as it is my first time using this ORM I am with some doubts. I have the code below to make a query in the database through an input type text but I am using the…
-
5
votes2
answers1131
viewsSQL QUERY using MAX function
I’m having a problem with a basic consultation of SQL, is for a migration of database. The query aims to bring data from the table of ASSOCIADOS/CLIENTES, but beyond this table I have another…
-
5
votes2
answers2215
viewsWhat is the difference between using a "Constraint" in "Foreign key" or not?
When creating a table in Mysql I used to create fields CONSTRAINT FOREIGN KEY how are you following to create the foreign key: CREATE TABLE socio ( id_socio INTEGER NOT NULL, nome VARCHAR(256) NOT…
-
5
votes1
answer695
viewsSELECT within variable in PROCEDURE
I’m not getting the result of a select within a variable. I want to add the column turno inside my WHERE. The problem is I get the value of @turnos in the following 3 formats: 1 - 'A' 2 - 'A','B' 3…
-
5
votes2
answers1140
viewsHow to make an Insert with columns and values coming from a select?
Imagine the following situation: Run a query in a database to acquire a particular record line, and then enter it again in the same table at the end of the records. It is possible that I can enter…
-
5
votes1
answer211
viewsMysql use 'as' in aliases or not?
Writing a query in Mysql I came across this doubt, I made a search, however, I found nothing (or did not know how to look for), the doubt is simple, there is some difference between using or not the…
-
5
votes1
answer44
viewsConfiguration of the Hibernate!
Hello I’m having a difficulty regarding a Hibernate configuration hibernate.hbm2ddl.import_files i believe q am doing everything right to find the sql path, I am using the same to insert some data…
-
4
votes2
answers1688
views1005 - Can’t create table '' Rrno: 150
I’m starting my programming journey and came across a mistake in the creation of the Mysql database: 1005 - Can’t create table 'mydb.supplier' (Rrno: 150) What could it be? I’ve entered several…
-
4
votes4
answers1407
viewsPrioritize word in SQL query - Mssql
I have the following task: Make a query where I have to give preference to the word passed as parameter. I search two fields with one OR, but I need to prioritize the consultation by the first…
-
4
votes2
answers690
viewsWhat is the cause of using SELECT null FROM RDB$DATABASE
I don’t understand this SELECT null FROM RDB$DATABASE: SELECT CK.IDCHEK, CK.DESCHE, (SELECT null FROM RDB$DATABASE) AS ENTEGU FROM TC_CHECKL CK ORDER BY CK.IDCHEK It looks like it goes in an…
-
4
votes5
answers620
viewsHow to select records that have a relationship with all values in a list?
I have the following sql: SELECT DISTINCT cp_pessoa.id, cp_pessoa.nome FROM cp_pessoa LEFT JOIN cp_habilidade_freelancer ON (cp_habilidade_freelancer.id_freelancer = cp_pessoa.id) LEFT JOIN…
-
4
votes4
answers2220
viewsHow to select "1" or "2" depending on the column value, in PL/SQL?
I have a column in my TIPOCLIENTE table. It shows CPF for Individuals and CNPJ for Legal Entities. I wish at the time of SELECT come "1" to CPF and "2" to CNPJ. How can I do this?…
-
4
votes2
answers234
viewsUpdate on Zend 2
I’m studying Zend Framework 2 and I’m needing a hand to do an update on two tables that are related. Come on. I have the table entries with the fields: id_entrada id_notafiscal and in the rating…
-
4
votes1
answer605
viewsRecover Corrupted . gz File
I have a file in . gz which is corrupted. Is it possible to extract data from this file? Even if it is partially? This file is a sql database dump. Thanks.
-
4
votes4
answers2880
viewsIs it possible to reference a column that is not a primary key in another table?
For example, I want to reference the id_2 of Table 1 in Table 2, I know I can do with primary keys but with other columns I do not know if it is possible. Table 1: id (primary key of Table 1), id_2,…
-
4
votes1
answer226
viewsHow to make "OR" between Subquerys using LINQ C#
I would like to implement with LINQ C# (Using Nhibernate as ORM), the following query: return (from t1 in Session.Query<Tabela1>() join t2 in Session.Query<Tabela2>() on t1 equals t2.T1…
-
4
votes4
answers33983
viewsHow to copy column data from one table to another table
I have a table called UC which has an email column, and I have a backup table of it. I need to copy the data (emails) from the table’s email column backup for the column email table uc, but I don’t…
-
4
votes1
answer1133
viewsProblem reading words from database with accent or cedilla
I’m reading my table categories from the database and when I have words with cedilla or accents, they appear in my project with strange characters. For example, in the categories I have a title…
-
4
votes2
answers3360
viewsBest practices for creating and reviewing procedures
What is the best way to create, change and evaluate procedures? Where work we do of the following flow: We check if the trial exists if we delete it and then create again. To Verse I use a header…
-
4
votes1
answer287
viewsPerformatively speaking, is it good to use Self Join?
Regarding this question: What good is a "self Join", technically speaking it’s performative to do this, or using 1N, 2N and 3N (normal shapes) is the best way? Just remembering that I never claimed…
-
4
votes2
answers1073
viewsConcatenate table name loop sql server
I’m trying to fill a table of mine with a loop in sql server follows the code: declare @i int set @i =1 while @i < 5 begin INSERT INTO TABELA VALUES('teste') set @i = @i + 1 end I would like to…
-
4
votes1
answer523
viewsUsing hexadecimal as ID in the database
Through the navigation bar, we realize that Google uses hexadecimal values to identify records: The print above was taken from Gmail, but the same occurs for other services like Google Drive. What…
-
4
votes3
answers3897
views -
4
votes5
answers6102
viewsHow to create a view in mysql by taking data from 3 tables and repeating the different columns in the result?
I have the tables: +----------Tabela-1--------+ id | nome | cnpj | qtd1 1 | carlos | 0563233 | 4 +--------------------------+ +----------Tabela-2--------+ id | nome | cnpj | qtd2 1 | carlos |…
-
4
votes2
answers5267
viewsHow to damage a website using XSS/SQL Injection?
I’m no kind of hacker, but I know some techniques. I’m training security and I got really thoughtful about it. Every time I inject a script, it’s a alert(), nothing dangerous to the host (for…
-
4
votes2
answers1637
viewsQuery SQL with 2 subselects for query lambda (or Linq)
select fieldA from TableA where fieldA = 459249 and fieldB in ( select FieldC from TableB where FieldD in ( select cte.FieldE from TableC cte where key= 'ABDC123' ) ) I managed to get to the point…
-
4
votes2
answers1970
viewsUse PIVOT operator without aggregation
I have the FAULTS table with the following columns: CREATE TABLE [dbo].[FALTAS]( [Id] [int] IDENTITY(1,1) NOT NULL, [Matricula] [int] NOT NULL, [Dia] [date] NOT NULL, [Situacao] [varchar](50) NOT…
-
4
votes2
answers1134
viewsIs there a performance gain when using View’s in SQL?
As View's are virtual tables resulting from consultations SQL, as in the example: CREATE VIEW view_name AS SELECT column_name(s) FROM table_name WHERE condition From this, we can update or delete a…
-
4
votes2
answers375
viewsJoin of SQL tables
I have a small problem when doing a Join between two tables in SQL, I wonder if you can help me. Are 2 tables GTM_ITEM_CLASSIFICATION and ITEM_REFNUM, both reference the table ITEM, but using…
-
4
votes1
answer826
viewsIs SQL Query in Loops a good practice?
Ride query with Where or other sql commands within loops like Foreach is it a good practice or not recommended at all? There is a better way to treat the data without having to make so many calls in…
-
4
votes2
answers508
viewsWhen is it recommended to use decreasing indexes?
For default, relational databases create indices using increasingly ordered binary tree structures. But there is the possibility of creating it in a decreasing way too. My question is whether (and…
-
4
votes3
answers33401
viewsSelect 1 record of each ID based on the last date
I have a fictitious table TB_META with the fields: COD_VENDEDOR | DAT_ATIVACAO | VAL_META Supposing I have these values in the table: 1 | 2011-01-01 | 1 1 | 2014-04-04 | 2 2 | 2012-01-01 | 3 2 |…
-
4
votes1
answer138
viewsSQL method for login
I have the following scheme: JdbcRowSet myrs = new JdbcRowSetImpl(); myrs.setUrl("jdbc:postgresql://localhost:9999/teste"); myrs.setUsername("postgres"); myrs.setPassword("");…
-
4
votes1
answer1838
viewsHow to Use "Not Exists" in LINQ?
I am trying to translate the query below, in LINQ, but without success, someone can help me? SELECT * FROM PESSOAL A WHERE NOT EXISTS ( SELECT Chapa FROM PRODUCAO B WHERE B.Chapa = A.Chapa AND…
-
4
votes1
answer85
viewsProblem in Mysql query
I’m having a problem with a Mysql query. In it I have 3 tables I need to cross: Table "drawing": id concurso_id customer_id Table "competition": id titulo criterioDiasCorridos criterioNotaMedia…
-
4
votes2
answers851
viewsIs it possible to limit the number of lines in a class attribute that is a list via JPQL?
I have the query below in JPQL FROM User u INNER JOIN FETCH u.enderecos e WHERE u.id =:id ORDER BY e.id DESC A user can have more than ten addresses, so we want to bring only ten addresses and if…
-
4
votes3
answers1909
viewsError creating a Mysqli database via PHP on Wampserver
I’m having trouble creating the database, on the net I found little content on, I only find how to build database by phpMyAdmin, but it is via script that I want to create. I have the following…
-
4
votes2
answers970
viewsTime between (between) "start time" and "end time"
I’m having trouble picking up the current time, in case I’m using CURTIME(), need to fetch the results that the current time, is between the value(time) of the column initial time and of final time,…
-
4
votes1
answer1518
viewsSQL Server Performance Loss with Sequential Insert
I have an . Net C# application that takes data via Web Service and saves it in an SQL Server 2012 database. Initial performance is 20 records per second. After having some 10000 records in the…